hongjun-7's blog

By hongjun-7, history, 9 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?
»
9 years ago, hide # |
 
Vote: I like it -29 Vote: I do not like it

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

»
9 years ago, hide # |
 
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.

»
9 years ago, hide # |
 
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.