This might work because of how your vendor happens to implement std::set and std::set::iterator, but it's not guaranteed to. Standard section 23.1.2 Paragraph 7 and Table 69 say that the expression a. Erase(q) is valid when a is an object of an associative container class type (set is an associative container) and "q denotes a valid dereferenceable iterator to a".
When the iterator is not from the container, you get Undefined Behavior. Appearing to work is one valid result of Undefined Behavior.
1 +1 for quoting the standard. – Macke Dec 8 '10 at 22:20 1 Beware of writing code that relies on undefined behavior. It never ends well.
– Mark Ransom Dec 8 '10 at 23:12 Thanks for this really clear answer! I actually didn't even use this code, but this strange behaviour really got me thinking, so I needed to ask :) Quick question, though: Where can I find the standards? I've seen many quotes, but still couldn't find it.
– pedromanoel Dec 10 '10 at 19:59 @pedromanoel: parashift. Com/c++-faq/big-picture. Html#faq-6.13 – aschepler Dec 10 '10 at 20:44.
I'm fairly convinced that iterators from different containers should not be mixed in this way, and that it results in the dreaded undefined behavior. So, what you're seeing is pure luck and chance, that works on your compiler's STL implementation. I wouldn't bet on it working anywhere else.
Std::set set_a; std::set set_b; std::set* current_set = &set_a; std::set::iterator it = current_set->find(o); std::set::iterator end = current_set->end(); if (it == end) { current_set = &set_b; it = current_set->lower_bound(); end = current_set->end(); if (it == end || *it! = o) { it = current_set->insert(it, o); } } // do calculations with it if (/* condition met */) { current_set->erase(it); }.
It looks like you're trying to reseat current_set, and I think you'll end up actually calling operator =() which will overwrite set_a with set_b. – Wyatt Anderson Dec 8 '10 at 22:35 Oops. I should really do my learning by asking questions, rather than attempting to answer them, I guess.
Edited answer to use pointer instead of reference... how's that? – Tony Park Dec 10 '10 at 0:50.
This might work because of how your vendor happens to implement std::set::iterator but it's not guaranteed to.
I'm fairly convinced that iterators from different containers should not be mixed in this way, and that it results in the dreaded undefined behavior.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.