Richard Richard - 1 month ago 11
C++ Question

c++: Behavior of push_back() and back() with 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?

Answer

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

Comments