int main() { { //Wrong set s = {1,2,3,4,5}; auto it = s.lower_bound(3); s.erase(it); it++; printf("%d\n", *it); } { //Correct 1 set s = {1,2,3,4,5}; auto it = s.lower_bound(3); s.erase(it++); printf("%d\n", *it); } { //Correct 2 set s = {1,2,3,4,5}; auto it = s.lower_bound(3); it = s.erase(it); printf("%d\n", *it); } }
No offense but this is very basic. This doesn't deserve a blog.
:(
It does. People who aren't too familiar with the STL (aka me) can get so frustrated when they don't know that they wrote a bug like this.
Thanks, i don't know this, but i think logic is it++ go and then delete, erase return next iterator and if you do erase and then change iterator, it will be in garbage.
Huh, it's a bit surprising to me that second way works. But I generally try to omit commands that are trying so hard to execute more than one command at once like "return a=b;" or "x=y++" and that is a very good example why. They introduce unnecessary confusion and bring no good except few characters shorter code.