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
6
votes
2 answers

Using negation of UnaryPredicate in erase-remove idiom

Consider the following scenario: bool is_odd(int i) { return (i % 2) != 0; } int main() { // ignore the method of vector initialization below. // assume C++11 is not to be used. std::vector v1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9…
CinCout
  • 9,486
  • 12
  • 49
  • 67
6
votes
1 answer

Remove items from vector, and mutate those which are removed

I have a std::vector> from which I want to erase-remove items matching some predicate. The removed objects should have a method called which sets some status for use elsewhere. Is there a reason I should not do this in the…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
6
votes
1 answer

boost bind compilation error

class A { bool OutofRange(string& a, string& b, string c); void Get(vector & str, string& a, string& b); } void A::Get(vector & str, string& a, string& b) { str.erase( std::remove_if (str.begin(), str.end(),…
aajkaltak
  • 1,437
  • 4
  • 20
  • 28
5
votes
1 answer

Why is 'resize-remove' faster than 'erase-remove' on vectors?

There is an 'erase-remove' idiom in C++ when it comes to removing several elements from containers, and there are discussions about an alternative 'resize-remove' way, e.g., here. People say that 'erase-remove' is better than 'resize-remove', but…
rhanqtl
  • 91
  • 5
5
votes
4 answers

Is there a safe (defined behaviour) way to use the STL to reduce the boilerplate of efficiently filtering a vector based off its indices?

I very often find myself wanting to filter a vector based on its index rather than its values. auto some_values = std::vector{1, 0, 4, 6, 2}; // Somewhere I figure out which items to remove. // This may be from user interaction or something //…
Elliott
  • 2,603
  • 2
  • 18
  • 35
5
votes
2 answers

Where is the performance gain of the erase-remove idiom coming from

I need to erase all elements from a vector which fulfill a certain criteria. My first approach would be to loop through the vector and call vector::erase on all elements which fulfill the criteria. As far as I understand, vector::erase has a bad…
Jounathaen
  • 803
  • 1
  • 9
  • 23
5
votes
3 answers

Accessing for_each iterator from lambda

Is it possible to access the std::for_each iterator, so I can erase the current element from an std::list using a lambda (as below) typedef std::shared_ptr EventPtr; std::list EventQueue; EventType evt; ... std::for_each( …
fishfood
  • 4,092
  • 4
  • 28
  • 35
4
votes
2 answers

How to remove whitespace in a sentence using remove command in C++?

I get a file as input and I read the first line like this (quotes mark the begin and end, but are not in the file): " 1, 2.0, 3.0, 4.0 " When I use the remove command like this: astring = line; cout…
ebe
  • 41
  • 1
4
votes
1 answer

Why do I get a different result if instead of x I directly pass array element ar[0] in std::remove function?

#include #include #include #include using namespace std; int main() { vector ar = {1, 2, 2, 2, 3, 4, 5, 5, 5, 6, 7}; vector sum; int n = ar.size(); for (int i = 0; i < n; i++) { …
saurov deb
  • 41
  • 2
4
votes
1 answer

How can I adapt the erase-remove idiom to work with vector tuples?

I have a vector of tuples with members j, k, and l. I am trying to adapt the erase-remove idiom such that I can remove an entire tuple if the value of the .k member meets a certain condition. I tried using the standard .erase(removeif()) methodology…
4
votes
1 answer

Why is a convenience helper for the erase-remove-idiom not provided by the standard?

Removing items from a collection in the STL requires a technique used so often that is has become an idiom: the erase-remove-idiom One of the most common usages of this idiom is to remove an item of type T from a vector std::vector
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
4
votes
6 answers

In C++, how can I delete all zeros except for x of them in every run of consecutive zeros within a list?

For every run of x or more consecutive zeros in a list in C++, I would like to delete all zeros in the run except for x of them. If x = 0, then delete all zeros. I was thinking of a C++ function that took a list, list L, and a number, int x, as…
b_ron_
  • 197
  • 1
  • 1
  • 10
3
votes
1 answer

Issues with pointers in c++ erase/remove algorithm

I have been trying to create a C++ program to find all the prime numbers under a certain integer, n, using the Sieve of Eratosthenes algorithm, which goes as follows: Create a vector of integers ranging from 2 to n. Start with the smallest element…
3
votes
4 answers

Is there a way to use the erase-remove idiom in concert with other looping conditions?

I have a function that, given a block of text, should remove all punctuation characters, and make all letters lowercase, and eventually, should shift them according to a mono alphabetic cipher. The code below works: class Cipher { public: …
adam tropp
  • 674
  • 7
  • 22
3
votes
3 answers

erase-remove_if idiom - was anything removed?

I'm creating an API which users will call to remove items from an internal vector. They will pass in criteria to search the vector for elements to remove. I'd like my API to return a boolean for if any elements were found and removed. I'm planning…
Brad
  • 5,492
  • 23
  • 34