Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++: Behavior of push_back() and back() with pointers

Tags:

c++

list

pointers

I have two C++ lists, std::list<T> List1 and std::list<T*> List2;. Now, I'd like to do the following operation several times:

List1.push_back(new_object);
List2.push_back(&List1.back());

My question: Does the reference in List2 stay valid, after each step? I.e.: Is the first element in List2 still referring to the first in List1 etc?

like image 344
Richard Avatar asked Oct 31 '16 11:10

Richard


1 Answers

Yes, it stays valid. std::list insertion doesn't invalidate iterators (or pointers to the content in this case).

like image 96
StoryTeller - Unslander Monica Avatar answered Nov 13 '22 22:11

StoryTeller - Unslander Monica