set of iterators?

Jul 2, 2012 at 3:24pm
dear all,

I am trying to use a set of iterators, like this

set< map<class_a*,class_b*>::iterator > todelete;

the compiler does not accept it. Does anyone know why?

many thanks in advance
Jul 2, 2012 at 3:52pm
Your compiler probably complains because iterators don't have ordering nor are hashable.
Anyway: Storing a mutable object in a set? Are you crazy? :D

Jul 2, 2012 at 4:13pm
The elements you store in the set must support operator< or overload of std::less. There is no operator< for map iterators so that's why it doesn't work.

rapidcoder wrote:
Storing a mutable object in a set? Are you crazy?

They are not mutable. You can't get access to non-const references to the elements in the set unless you use const_cast.
Last edited on Jul 2, 2012 at 4:13pm
Jul 2, 2012 at 5:04pm
ok, thanks both of you for the clear answers!
Jul 2, 2012 at 7:55pm

They are not mutable. You can't get access to non-const references to the elements in the set unless you use const_cast.


Wrong. Const != immutable. Iterators are inherently mutable / stateful by definition. That you can't modify them through the set API, doesn't mean you can't do it in some other way.
Last edited on Jul 2, 2012 at 7:58pm
Jul 2, 2012 at 8:27pm
Are you talking about that the iterators can be invalidated while being in the set?
Jul 5, 2012 at 7:57am
Not only invalidated, but just modified in any way affecting equality / ordering / hash. E.g. just calling ++ on them. How do you compare / hash iterators? Because there is no sane way to do that, STL doesn't provide ordering / hashing for iterators by default. They know it would be a very bad idea to use them in sets / map keys and it would easily break in very nasty ways.
Last edited on Jul 5, 2012 at 7:59am
Topic archived. No new replies allowed.