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

How do I delete all the even/odd indexed elements from a vector in C++?

I am new to STL and was trying a simple program to insert elements using push_back and trying to remove even indexed elements. I took n elements and pushed it into the vector. But when I erase it I either get segmentation fault or some undesired…
dread_user
  • 21
  • 3
-1
votes
3 answers

How to erase a player with a given name from a vector

I'm seeing some big bad error messages whilst trying to erase a string from a vector. I'm trying to use the erase-remove idiom but it's not working. Here are some snippets to show my predicament. team.players.erase( remove(team.players.begin(),…
Lachy Vass
  • 51
  • 1
  • 6
-1
votes
1 answer

How to check if an erase-remove operation has been successful?

This is the first time I use this idiom and I have a vector v1 which elements must be a subset of the elements in another vector v2. Now, imagining v1 and v2 as sets, I want to perform v2-v1 and throw an exception if v1[i] doesn't exists in v2 (for…
justHelloWorld
  • 6,478
  • 8
  • 58
  • 138
-1
votes
2 answers

vector erase specific indexes with iterators (not based on range or condition)

Say, I have two vector as follows in the code, I want to erase the elements indexed by vector "index_to_filter" in the vector "data" using iterators. The dummy way in the code is just to point the obvious error. So far, I couldn't get it working,…
Vtik
  • 3,073
  • 2
  • 23
  • 38
-1
votes
2 answers

remove duplicate using method

I build removing duplicates, however I'm thinking how to use a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) write a test program…
Young
  • 1
  • 1
  • 3
-2
votes
1 answer

Can't remove element by ID from vector of non primitive datatypes

I have a vector of my user-defined data type city. I'm trying to remove an element from this vector by its ID; eventually all cities will be removed from this list in a while(!cityList.empty()) loop. I plan to use the erase-remove idiom to…
Jthami05
  • 101
  • 3
  • 12
-4
votes
1 answer

Lambda expression taking an unique pointer contained in a vector

I have a vector foos of unique pointers to my resource type Foo. I would like to delete some of these that satisfy a given condition. How should I do this ? class Foo { public: int some_num; Foo(int some_num_) :some_num{ some_num_ } {} …
rranjik
  • 690
  • 9
  • 21
1 2 3 4 5 6
7