Re: Strange behaviors of Iterator for set
 
Bo Yang wrote:
Hi,
  Today, I make some test on the C++ STL iterators of set containers
with GNU g++ compiler. The code is:
#include <set>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
 set<int> ms;
 int i;
 for (i=1; i<10; i++)
   ms.insert(i);
 set<int>::iterator it = ms.begin();
 it++;
 ms.erase(it);
 cout << "Deleted: " << *(it) << endl;
Undefined behaviour.  You're trying to dereference an invalid
iterator.
 set<int>::iterator ii = it;
You're constructing another iterator and initialising it from
the invalid iterator; the 'ii' becomes invalid immediately.
 cout << "++: " << *(++ii) << endl;
 cout << "+2: " << *(++ii) << endl;
 cout << "--: " << *(--it) << endl;
 it++;
 cout << "--++: " << *(it) << endl;
All this is undefined behaviour, by itself and due to the
preceding undefined behaviour.
 ms.insert(2);
 it = ms.begin();
 it++;
 it++;
 it++;
 it++;
This is all fine.  'it' is valid still, because your set
contains more than 4 values.
 ms.erase(it);
 cout << "Deleted: " << *(it) << endl;
 ii = it;
 cout << "++: " << *(++ii) << endl;
 cout << "+2: " << *(++ii) << endl;
 cout << "--: " << *(--it) << endl;
 it++;
 cout << "--++: " << *(it) << endl;
Here you go again...
 it = ms.end();
 it--;
That's fine.  'it' refers to the last element in the set.
 ms.erase(it);
'it' now is invalid.
 cout << "Deelted: " << *(it) << endl;
 cout << "++: " << *(++it) << endl;
 cout << "+2: " << *(++it) << endl;
And again, you're using an invalid iterator.  Undefined
behaviour.
 return 0;
}
and the output is:
Deleted: 2
++: 1
+2: 3
--: 1
--++: 3
Deleted: 5
++: 6
+2: 7
--: 6
--++: 7
Deelted: 9
++: 8
+2: 7
I find that, when I erase something, the whole iterator's behavior is
unpredicted.
Of course.  When you erase the element through an iterator, the
iterator becomes *invalid*.  It has no behavior defined by the
language.
I can't make sure what is a next ++ is in a set unlike I
am sure with a vector...
I don't understand that sentence.  With all standard containers
if you erase the object, the iterator that used to refer to the
deleted object becomes *invalid*.  What ++, what vector?
Does this a right behaviors?
Yes, it does, I guess.
And when I use the remove_if and many
other algorithm on set, it will make some crash, why?
Don't know.  Show the code, then we can talk.
V
-- 
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask