hongjun-7's blog

By hongjun-7, history, 7 years ago, In English
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);
	}
}
Tags set
  • Vote: I like it
  • +132
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it -29 Vote: I do not like it

No offense but this is very basic. This doesn't deserve a blog.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    :(

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +55 Vote: I do not like it

    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.

»
7 years ago, # |
  Vote: I like it -15 Vote: I do not like it

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.

»
7 years ago, # |
  Vote: I like it +26 Vote: I do not like it

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.