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

Problem with removing unique_ptr from vector

I am struggling with removing a unique_ptr from vector. I have a vector: std::vector>; which I am filling with certain number of Agents. I do it in the following way: In class constructor…
Michael S.
  • 155
  • 1
  • 11
0
votes
3 answers

How to erase value from vector of struct using erase-remove idiom?

How to erase all values from vector of struct, where struct value k equals to 0? struct tabuRecord { int x; int y; int k; tabuRecord( int x, int y, int k) : x(x), y(y), k(k){} }; vector tabu; v.insert(v.begin(), tabuRecord(1, 2,…
djankooo
  • 21
  • 6
0
votes
3 answers

C++ nested for loop with erasing elements

I would like to check all Elements of an vector against each other. By checking a condition an element should be removed. One approach was to erase the elements by nested for loops for (int a = 0; a < rs.size(); a++) { Point A = rs[a]; for…
manuel
  • 1,127
  • 1
  • 8
  • 15
0
votes
1 answer

Generic Function remove() vs Member Function remove() for linked list

I'm reading book "The C++ STL. A Tutorial and References" written by Nicolai M.Josuttis and in one of the chapters devoted to STL algorithms author states the following: If you call remove() for elements of a list, the algorithm doesn’t know that it…
hma_hma
  • 9
  • 2
0
votes
1 answer

Removing chars from an array of chars in C++

I am trying to create a way to remove a substring from a string. It is in char form so I can't use convenient means of that are gleefully given to strings. I know how to search for it, I can locate the index but man deleting a char has been a bit…
user6378390
0
votes
1 answer

What happens to the element when remove_if is used?

I'm trying to understand how remove_if works (the << is overloaded) and for that I want to remove_if all the strings which begin with 'C': vector langs = { "Python", "C++", "C", "Java", "C#" }; cout << "Initial vector: " << langs << " | Size…
alekscooper
  • 795
  • 1
  • 7
  • 19
0
votes
1 answer

remove_if erases everything from vector

I'm writing a program for an assignment - it's supposed to be a database for information about employees in a company. Basically, a vector containing structures (individual employees). The trouble I'm having is that remove_if erases everything…
0
votes
1 answer

C++ Erase-remove idiom with object

Edit: @Holt helped me, the solution is to pass an instance of Engine if hasCollided is non-static: std::bind(&Engine::hasCollided, this, ball, _1); I have a function which returns true or false whether a brick is hit by a ball. I want to erase a…
user5446899
0
votes
0 answers

Removing punctuation from txt.file/string in C++

Hello fellow programmers, I have a question that hopefully can be answered by one of you. The following code states that after the txt.file is put into a string variable, the entire string (array of characters) will then be removed of any…
0
votes
2 answers

having trouble with vector.erase() and remove_if()

Prior to using remove_if, I was using remove. So say if I had vec = {1, 2, 2, 2, 5}, and wanted to remove all 2s, I would do: for (vector::iterator it = vec.begin(); it!= vector.end(); ++it){ if (*it == 2){ …
jason adams
  • 545
  • 2
  • 15
  • 30
0
votes
0 answers

Segmentation fault while parallelizing STL List erase function

I am trying to parallelize my C++ code using Open MP but I am running into Segmentation fault, the serial version is running fine. I used gdb debugger and I know the error is occurring while erasing one of the elements of the STL List. In the code…
Rohit
  • 11
  • 3
0
votes
3 answers

C++ Marking objects for removal in STD list via nullptrs

I was wondering if this is an accaptable practice: struct Item { }; std::list> Items; std::list> RemovedItems; void Update() { Items.push_back(std::make_shared()); // sample item for (auto…
Grapes
  • 2,473
  • 3
  • 26
  • 42
0
votes
3 answers

Erasing elements in a vector

So I have a vector of unsigned ints (vector is called vector1). I have another vector of a struct I created (vector is called vector2). vector holds an integer that is the index of the vector. For example, let's…
OGH
  • 540
  • 1
  • 4
  • 15
0
votes
1 answer

Is this a good way to store, iterate and delete pointers in an std::vector?

#include #include #include #include #include using namespace std; struct delete_ptr { template void operator()(T*& t) { delete t; t = 0; } }; struct…