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
3
votes
3 answers

Erase in a loop with a condition in C++

Is there a better way to write: for (auto i = container.begin(); i != container.end();) { if (condition(i)) { i = container.erase(i); continue; } ++i; } This code does what I want, but it feels like bad style. How can…
Fractale
  • 1,503
  • 3
  • 19
  • 34
3
votes
1 answer

Is it possible to extend the "erase–remove" idiom to work on multiple containers at once?

In C++, the erase-remove idiom is a great way to delete all elements of a standard container that satisfy a given criterion. Is it possible to extend the erase-remove idiom to work on multiple containers at once? That is, can one invoke something…
3
votes
1 answer

In c++, is it safe to use std::numeric_limits::max() as a special "flag"?

Given std::vector a; std::vector ind; where ind is 1sorted in ascending order. I want to do the equivalent of the following: for (auto it=ind.rbegin();it!=ind.rend();it++) a.erase(a.begin() + *it); I came up with this: for (auto…
Jens
  • 153
  • 1
  • 8
3
votes
1 answer

Using erase-remove idiom on non-unique collections

#include #include #include int main() { enum class En{A, B}; std::vector vec{En::A, En::B, En::A, En::B, En::A, En::B, En::A}; for(const auto& i : vec) std::cout << int(i) << ", "; std::cout <<…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
3
votes
3 answers

Erase-remove idiom: what happens when remove return past-the-end-iterator?

I got this question when I was reading erase-remove idiom (item 32) from Scott Meyers "Effective STL” book. vector v; ... v.erase(remove(v.begin(), v.end(), 99), v.end()); remove basically returns the "new logical end” and elements of the…
aJ.
  • 34,624
  • 22
  • 86
  • 128
2
votes
4 answers

std::remove doesn't behave as expected

While I was debugging, I figured out that std::remove doesn't behave as I expected. For example: std::vector nums {0,1,2,2,3,0,4,2}; int val{2}; std::remove(nums.begin(), nums.end(), val); nums vector changes to [0, 1, 3, 0, 4, 0, 4, 2]. Where…
kry23
  • 99
  • 5
2
votes
2 answers

Possible Logic Error: remove(vector.begin(),vector.end(),val)

I am looking for a way to delete certain lines in a file. I read on Stack Overflow that the best way to do this is to write it into a vector, and then remove the element from the array, and then read the array back into the file. I was doing this,…
That_Coder
  • 56
  • 7
2
votes
1 answer

Why std::erase and std::erase_if don't support projection?

C++ ranges are nice, but AFAIK they still "suffer" from the fact that they do not know to modify containers, e.g. if you use ranges::remove you still need to do container.erase(... Now there are algorithms that do know how to erase from containers…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
2
votes
4 answers

faster erase-remove idiom when I don't care about order and don't have duplicates?

I have a vector of objects and want to delete by value. However the value only occurs once if at all, and I don't care about sorting. Obviously, if such delete-by-values were extremely common, and/or the data set quite big, a vector wouldn't be the…
Swiss Frank
  • 1,985
  • 15
  • 33
2
votes
1 answer

Erase-remove idiom for deleting in a nested container? (deleting outer ones; C++ STL)

when i'm deleting from a non-nested container like a vector, i'm doing something like: struct is_to_remove { is_to_remove(dynamic_bitset<>& x) : x(x) {} const bool operator()(unsigned int id) { return x[id]; } private: …
sascha
  • 32,238
  • 6
  • 68
  • 110
2
votes
1 answer

unordered_map rehasing on erase()

I am not entirely clear on whether unordered_map is allowed to perform a rehash when doing an erase() It is pretty clear that rehashing can happen during insert() thus invalidating all iterators and…
imreal
  • 10,178
  • 2
  • 32
  • 48
2
votes
2 answers

A vector holds class objects, The class object contains 3 strings per object. How do i find the specific string, then delete the entire element?

I have a class that contains 3 elements for example {first_name, Last_name, Phone} I have a vector that holds this set of information. In what manner could I go about looking for a single element of the set, for example find(last_name), and delete…
Michael La
  • 51
  • 5
2
votes
1 answer

Using erase-remove idiom for function

Stacked people. Iam trying to implement an observer(esque?) pattern for my program. I have a component which stores what functions should be called if an event occours. My prolem is that i dont know how should i erase my function from the…
val
  • 729
  • 6
  • 19
2
votes
2 answers

Remove range of elements from QHash

I use QHash as a container and I have a task to remove all items that satisfy the predicate. At first I thought to use the Erase-remove idiom it turned out that QHash has no option to delete the range, but only a function to delete a single element…
αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
2
votes
1 answer

erase-remove idiom problems for vector in a class

I am using class1 that contains a few governing variables and a vector of another class, class2. I would like to be able to flag various elements of the vector of class2 (using boolean variable 'delete_me') and then run the erase-remove idiom to…
Charlie
  • 319
  • 2
  • 5
  • 12