How to remove multiple elements of a vector in a loop
I was devekloping a program in c++ for an vector. My code is std::vector<int> isVector; isVector[0] = 4; isVector[1] = 5; isVector[2] = 6; in that I have remove certain value in the case of 1 and 2 under some condition the value is removed. I had used the following code for it.
Code:
for(j=0; j<isVector.size(); j++)
{
if(isVector[j] == 5 || isVector[j] == 6) {
isVector.erase( isVector.begin()+j );
}
}
Which give an output as follow
isVector.size() = 2
isVector[0] = 4;
isVector[1] = 6;
it is an fall through problem how should I remove it and I want to change the value of the vector recursively
Re: How to remove multiple elements of a vector in a loop
I had search for your problem. In isVector[j] == 4 in that the control is fall. Even I was facing the similar issue in my program when vectorint[j] ==5, isvector.begin()+i= 0+1 is getting erased. When isVector[j] == 6, isVector.begin()+j = 0+2 and isVector[3] is erased here as value for [2] is erased that’s why the value 5 seen in the list. Maybe this is because of the iterator. I don’t know how to change its dynamic position and I want to know how to remove the value of a vector recursively which should change the size of the vector.
Re: How to remove multiple elements of a vector in a loop
I think so you need a different loop. For that you just need to repeat the value of a j in the case where the value of j is removed. I had used this when I was using the large vector where I need to remove the value of any element. Maybe you just need to set the value of j as an 0 after the value id being removed and then again repeat from the top of the main part of the program. Hope this will work as it is for the large vector.
Re: How to remove multiple elements of a vector in a loop
You can use this in your program. When you are removing the value from the index you must use the erase-remove. You can erase the value from the loop by using the following program.
Code:
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <iterator>
using namespace std1;
bool IsOdd(int value)
{
return value%2 != 0;
}
int main()
{
vector<int> c;
c.push_back(1);
c.push_back(2);
c.push_back(3);
c.push_back(4);
copy(c.begin(),c.end(),ostream_iterator<int>(cout," "));
c.erase( remove(c.begin(),c.end(),3) , c.end() );
copy(c.begin(),c.end(),ostream_iterator<int>(cout," "));
c.erase( remove_if(c.begin(),c.end(),IsOdd) , c.end() );
copy(c.begin(),c.end(),ostream_iterator<int>(cout," "));
for (vector<int>::iterator is=c.begin(); is!=c.end();
{
if (*is == 4)
is = c.erase(is);
else
++is;
}
copy(c.begin(),c.end(),ostream_iterator<int>(cout," "));
return 0;
}
Re: How to remove multiple elements of a vector in a loop
Even I persist with the same issue. For removing the multiple elements from a loop it will need a extra iteration in a loop so that they won’t have more the 3 element you can try this in your for loop.
Code:
for(j=0; j<isVector.size(); j++) {
if(isVector[j] == 4 || isVector[i] == 5) {
int remove = isVector[j];
isVector.erase( remove(isVector.begin(), isVector.end(), remove), isVector.end() );
i = 0;
Just try this in your for loop hope this will help you out.
Re: How to remove multiple elements of a vector in a loop
Even I was facing the same sort of issue and had tried the couple of things but nothing had being compiled successfully. I’m searching something which makes the things simple for me and even I was searching for the documentation but I dint come up with anything. I had even tried a unary predict which take an element as a range of an arguments and then return the value as zero, false or truth. This is due to the pointer or the function whose class is overloading.
Re: How to remove multiple elements of a vector in a loop
For this you just need expressions to be translated. Usually unary predict which is an function takes one argument as an element and then arrange it as an case for which the function argument should be match exactly with the container which represent the production with either zero or false or it might be true value for some of the condition the function should return a Bool value don’t matter whether the element is removed or no. moreover This can be a pointer for a function whose class is overloading an operator () .