Questions tagged [erase-remove-idiom]

The erase-remove idiom is a common C++ technique to eliminate elements that fulfill a certain criterion from a C++ Standard Library container.

Instead of removing elements, remove operator puts elements, that doesn't match criteria, at the end of given range and then returns an iterator pointing one element past the last matching element. Then the erase member function deletes elements from returned iterator to the given one.

More information can be found at Wikipedia.

97 questions
1
vote
1 answer

remove arbitrary list of items from std::vector >

I have a vector of vectors, representing an array. I would like to remove rows efficiently, ie with minimal complexity and allocations I have thought about building a new vector of vectors, copying only non-deleted rows, using move semantics, like…
galinette
  • 8,896
  • 2
  • 36
  • 87
0
votes
0 answers

std::erase does not return an error when std::remove_if is used inappropriately in the erase-remove idiom

As we know, the erase-remove idiom can be used as a concise pattern to delete elements of a vector. In particular, std::remove_if swaps elements inside the vector in order to put all elements that do not match the predicate towards the beginning of…
0
votes
1 answer

Trying to remove all non alpha characters from a string using C++ what's the best way to do that given the code that I have?

I'm a beginner with C++ and not too familiar with the language yet. So what would be the simplest way to fix my code? I think there's something wrong with userInput.insert(i, ""); but I'm not sure what. Example: If the input is: -Hello, 1…
0
votes
1 answer

C++, fast removal of elements from vector by binary index

There are several posts focused on the fast removal of elements given by indices from a vector. This question represents a slightly modified version of the problem. There is a vector of elements: std::vector numbers{ 100, 200, 300, 400,…
justik
  • 4,145
  • 6
  • 32
  • 53
0
votes
0 answers

Why this program works? itrator does not invalidate after erase

I was doing this leetcode question of removing duplicates from sorted vector and return new size of vector, I came up with obvious simple approach to remove duplicates. but during my time of answering the question i found out erase() invalidate the…
meivinay
  • 9
  • 4
0
votes
2 answers

Remove duplicate elements from std::vector but starting from the front?

How can I delete duplicate elements from a vector but starting from the front? So 2 3 4 5 2 5 would become 3 4 2 5 1 5 3 1 would become 5 3 1 I would like the solution to be easy to read, and it would be nice if it had good performance as well.
binaryBigInt
  • 1,526
  • 2
  • 18
  • 44
0
votes
1 answer

How to remove certain specified value from vectors?

mySongs is a vector that store a collection of songs input by the user. In the if statement, the program will check the elements in the vector with user input. If match, it will delete that specified value from the vector. When I look for the…
user11848520
0
votes
2 answers

C++ vector erase function not working properly

I want to erase all elements with value greater 2 and less 5 here is the code : vector myvector{3, 3, 3, 3, 3, 3, 3, 1, 2, 3, 4, 5 , 2, 3, 4, 9}; vector::iterator it; it = myvector.begin(); for(int i = 0; i < myvector.size(); i++) { …
Villance
  • 41
  • 1
  • 9
0
votes
1 answer

remove element of struct from vector of struct with erase-remove-idiom

I have a vector of structs and I want to remove an element from the vector with specific values.I know how it can be done for for example a vector of int values using erase remove, but now sure how to do it for a vector of structs: #include…
Arsalan
  • 333
  • 4
  • 14
0
votes
2 answers

I'm trying to compare the 0th element of a vector with other elements to remove the other elements equal to it but I get an unexpected output

So I want to compare the 0th element of a vector with the other elements to see if they're equal because I want to remove other instances of that element's value from the vector e.g. {1, 1, 2, 3, 1} becomes {1, 2, 3} and this is the code I…
NotApollo
  • 39
  • 3
0
votes
1 answer

I am getting this ERROR: AddressSanitizer: negative-size-param: (size=-8)

When removing the given elements from the given array, I keep getting an error. This is my code, I am not sure where I the error is: int removeElement(vector& nums, int val) { int i=0; int size=nums.size(); if(size…
0
votes
1 answer

How to remove from vector of pairs the value item from pair that is a class in c++?

i am trying to remove all Orders that have old pizza than 3 days : i have this vector of pairs : std::vector> x; x.push_back(std::make_pair(Client(2,"Anca"),Order(3,1))); …
Ac1234
  • 67
  • 6
0
votes
1 answer

C++ Erase-remove Idiom on non-existant value in vector

I want to remove an element from a vector, but it is not guaranteed that the element exists. I have looked online but found no information regarding how the erase-remove idiom would treat a value that isn't in the container. My vector is declared as…
A.D
  • 427
  • 1
  • 4
  • 12
0
votes
2 answers

remove_if removes element even when predicate returns false?

I am writing an octree algorithm. Inside function I traverse octree. I get node pointer and Sphere as input. I check if node should hold sphere then I want to add it to nodes object list and remove it from its parents list. following is…
0
votes
1 answer

Why does `std::set::erase(const key_type&)` return `size_type` instead of `bool`?

Because std::set doesn't insert() duplicates, it's assured to contain unique elements. When using the overload erase(const key_type&), its object would have contained maximum 1 element of that same value. Hence, it may return either 1 (if present)…
iammilind
  • 68,093
  • 33
  • 169
  • 336