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

C++ Erase vector element by value rather than by position?

vector myVector; and lets say the values in the vector are this (in this order): 5 9 2 8 0 7 If I wanted to erase the element that contains the value of "8", I think I would do this: myVector.erase(myVector.begin()+4); Because that would…
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
55
votes
4 answers

Remove First and Last Character C++

How to remove first and last character from std::string, I am already doing the following code. But this code only removes the last character m_VirtualHostName = m_VirtualHostName.erase(m_VirtualHostName.size() - 1) How to remove the first…
Putra Fajar Hasanuddin
  • 1,101
  • 3
  • 13
  • 25
50
votes
4 answers

std::remove_if - lambda, not removing anything from the collection

Ok, I expect I've made a dumb mistake here. I have a list of DisplayDevice3d and each DisplayDevice3d contains a list of DisplayMode3d. I want to remove all items from the list of DisplayDevice3d that don't have any DisplayMode3d's. I'm trying to…
Robinson
  • 9,666
  • 16
  • 71
  • 115
30
votes
2 answers

Is there a better alternative to std::remove_if to remove elements from a vector?

The task of removing elements with a certain property from a std::vector or other container lends itself to a functional style implementation: Why bother with loops, memory deallocation and moving data around correctly? However the standard way of…
Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
26
votes
3 answers

Using erase-remove_if idiom

Let's say I have std::vector>. I am trying to use erase-remove_if idiom to remove pairs from the vector. stopPoints.erase(std::remove_if(stopPoints.begin(), stopPoints.end(), …
omegasbk
  • 826
  • 1
  • 10
  • 24
21
votes
1 answer

std::erase and std::remove combination to delete specific element doesn't work for specific example

#include #include using namespace std; int main() { vector a = {1,2,3,7,1,5,4}; vector b = {6,7,4,3,3,1,7}; a.erase(remove(a.begin(),a.end(),a[0]),a.end()); …
user3064869
  • 530
  • 1
  • 6
  • 19
20
votes
1 answer

Does C++ standard library provide more compact and generalized version of the erase–remove idiom?

We can erase one element/ entry from a container by the popular erase–remove idiom. However, many of us would have encountered some problems while applying this idiom: one can easily get into the pitfall of typos…
JeJo
  • 30,635
  • 6
  • 49
  • 88
17
votes
1 answer

For the erase-remove idiom, why is the second parameter necessary which points to the end of the container?

Consider the following code (taken from cppreference.com, slightly adapted): #include #include #include #include int main() { std::string str1 = " Text with some spaces"; …
j00hi
  • 5,420
  • 3
  • 45
  • 82
14
votes
4 answers

STL "erase-remove" idiom: Why not "resize-remove"?

It is commonly understood that a good way to fully delete desired items from a std::vector is the erase-remove idiom. As noted in the above link (as of the date of this posting), in code the erase-remove idiom looks like this: int main() { //…
Dan Nissenbaum
  • 13,558
  • 21
  • 105
  • 181
13
votes
2 answers

erase() after performing remove_if()

I've created a function to run through a vector of strings and remove any strings of length 3 or less. This is a lesson in using the STL Algorithm library. I'm having trouble in that the functions work but not only does it delete strings of length…
MCP
  • 4,436
  • 12
  • 44
  • 53
8
votes
3 answers

Is std::remove_if guaranteed to call predicate in order?

Will std::remove_if always call the predicate on each element in order (according to the iterator's order) or could it be called out of order? Here is a toy example of what I would like to do: void processVector(std::vector values) { …
Solaraeus
  • 440
  • 3
  • 10
7
votes
5 answers

Remove vector elements based on the index

I wanted to remove the elements of the vector based on the index, say all the even indexed elements. I have read about the erase remove idiom but can't see how to apply it. This is what I tried: vector line; line.reserve(10); …
nikhil
  • 8,925
  • 21
  • 62
  • 102
6
votes
1 answer

Removing elements marked for removal with Ranges-V3

I've got two vectors: struct MyData{ double value; }; std::vector remove_flags = {0, 1, 0, 0, 0, 0, 1, 0}; std::vector data = {{},{},{},{},{},{},{},{}}; The remove_flags vector contains an array of flags of the exact same…
Krupip
  • 4,404
  • 2
  • 32
  • 54
6
votes
1 answer

Why erase-remove idiom not working for reverse iterator

My aim was to try a solution for this question:Removing all empty elements in a vector from end. using erase-remove idiom. The idea is to remove all elements starting from the end which are empty (equal to white-space) in a given a…
JeJo
  • 30,635
  • 6
  • 49
  • 88
6
votes
4 answers

How to emulate remove_unless

I have code to remove all elements from a std::vector that are less than some int limit. I've written some functions that partially apply lambdas: auto less_than_limit = [](int limit) { return [=](int elem) { return limit > elem; …
erip
  • 16,374
  • 11
  • 66
  • 121
1
2 3 4 5 6 7