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
2
votes
1 answer

Should I be using erase-remove idiom here?

I have an std::vector (_pBkBuffer in the code below). It contains a number of static objects (at the start of the vector) that don't change, followed by a variable number of dynamic objects. // erase-remove, but leave the static…
fishfood
  • 4,092
  • 4
  • 28
  • 35
2
votes
1 answer

Removing fields in c++ vector

I cannot grasp the idea and programm such a thing correctly (I'm a beginner): I have a vector. Vector has lets say elements: fieldA, fieldB, fieldC. So having array of these vectors, I want to check, beginning with the last vector in the array, and…
berndh
  • 1,355
  • 4
  • 14
  • 19
1
vote
1 answer

Delete the selected item in forward_list C++

I need to somehow remove an element in a list if it is already in another list. I created a function but it doesn't work. Tell me how to fix it. thank you very much. void compare(forward_list list_1, forward_list list_2) { auto…
1
vote
1 answer

Crash when using erase–remove idiom

When i execute the code below containing erase–remove idiom , i get a crash (segmentation fault). Id like to know what is the problem. class One { public: One() {} void print(){ std::cout << "print id: " << id << "\n"; } void setId(int…
black_gay
  • 143
  • 7
1
vote
3 answers

Removing duplicates from an vector is giving unexpected results

Given a vector of integers, iterate through the vector and check whether there are more than one of the same number. In that case, remove them so that only a single index of the vector contains that number. Here are a few examples: vector arr…
1
vote
3 answers

(Beginner) Don't know why code doesn't give correct output

Firstly, I'm trying to create a program which takes two char arrays and removes from the first any chars that shew up in the second array. I have added comments to make it easier to understand what's happening. #include void rcfs(char…
1
vote
4 answers

function that remove an integer from an array using pointer in C

I got a question where I need to write a function that reads an integer X and an array A of type int (size N) from the keyboard and eliminates all occurrences of X in A. for example the input is: 5 1 2 3 4 3 3 and it would return: A : 1 2 3 4 3 New…
1
vote
3 answers

Is erasing a range more efficient than erasing each of the elements separately?

If you have defined a range of elements you want to "erase", is it more efficient to call vector::erase(iterator) for every element in the range, or to call vector::erase(iterator,iterator once?
alexander.sivak
  • 4,352
  • 3
  • 18
  • 27
1
vote
1 answer

Erase remove idiom on a std::string with non-printable characters throws exception

I am reading some text from another process through a pipe. The pipe returns the data to the variable chBuf but contains lots of non-printable chars. I tried to remove these non-printable characters using erase and remove but throws an exception.…
ark1974
  • 615
  • 5
  • 16
1
vote
2 answers

To remove double quotes from vector c++ 98

I am trying to remove the double quotes characters from my vector string value. for eg: if my vector string has values (50,40,"50GB","40GB",60). i am trying refine the vector by erasing Double quotes(" "). so the my final vector…
AK90
  • 428
  • 1
  • 4
  • 16
1
vote
1 answer

Is there an equivalent of the erase-remove idiom in Javascript?

If I add or delete an element while iterating over an Array or an Array-like object in Javascript, do I need to take into account that the underlying collection may have changed or that the iterators may have become invalidated? In other words, is…
mallwright
  • 1,670
  • 14
  • 31
1
vote
3 answers

call a function, inside a for loop, to remove elements in a vector (or list)

vector::iterator it; for(it;it!=vector_of_node.end();it++){ if(it->get_name()=="MARIA"){ vector_of_node.erase(it); } i hope the goal of my code is clear. i want to eliminate multiple objects (which are described in a class called…
1
vote
2 answers

Erase all characters in string between the first parenthesis "(" andthe last parenthesis "(" including these parentheses C++

I have a trouble to remove all the characters between the first parenthesis "(" and the last parenthesis "(" including them. Here is the test program I use to make it work, but without success... #include #include using…
1
vote
1 answer

C++ removing non-alphabetic chars from beginning of a word

I have the following code in a method of string type which reads words from a file, removes all non-alphabetic chars (as shown below) from the beginning of the word and returns them after it does so. After printing out the results in the main…
1
vote
3 answers

Single statement method to remove elements from container

Is there a single algorithm that removes elements from a container as happens in the following code? vec_it = std::remove_if( vec.begin(), vec.end(), pred ); vec.erase( vec_it, vec.end() );
Patrick
  • 8,175
  • 7
  • 56
  • 72