In std::list an insertion inside the container should not invalidate an iterator but how is that implemented? For example I have a list and insert one value:
std::list<int> list;
list.push_back(12);
Then I get the end iterator:
auto end = list.end();
auto it = --end;
Now end should point to the inserted element. What happens when I keep the end iterator and do another insertion? Will --end still point to 12 or to the new inserted value?
Edit1:
I tested this with the following program:
#include <iostream>
#include <string>
#include <list>
int main()
{
std::list<int> list;
list.push_back(12);
auto end = list.end();
auto it = std::prev(end);
std::cout << *it << std::endl;
list.push_back(13);
it = std::prev(end);
std::cout << *it << std::endl;
it = std::prev(end);
std::cout << *it << std::endl;
}
This gives me 12 13 13 as result which I don't understand because I did not edit the end iterator after the second insertion. This means the container has access to the iterator internals inside push_back?