On Nov 23, 11:42 pm, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
The correct code
requires careful use of a postfix increment operator:
set<int>::iterator rit = r.begin();
while (rit != r.end()) {
if (*rit % 2 == 1)
r.erase(rit++);
else
++rit;
}
Seems you are goddam right
but ouch I can't still tell the difference
and personally I'd better use the commented method:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
using namespace std;
void print(int elem) {cout << elem << endl;}
bool is_odd(int x) {return x % 2 != 0;}
int main() {
set<int> r;
int tmp[] = {0,1,-5,-3,8,3,2,4,5,6,7,8,9,11,22,44,55};
r.insert(tmp, tmp + sizeof(tmp)/sizeof(int));
set<int>::iterator rit = r.begin();
while (rit != r.end()) {
if (is_odd(*rit))
r.erase(rit++);
else
++rit;
}
/*
while (true) {
set<int>::iterator rit = find_if(r.begin(), r.end(), is_odd);
if (rit == r.end()) break;
r.erase(rit);
}
*/
for_each(r.begin(), r.end(), print);
system("pause");
return 0;
}
each element deleted and then one more time. If the number of elements
solution becomes O(n*n) instead of O(n).
[ comp.lang.c++.moderated. First time posters: Do this! ]