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;
}
Bookmarks