STL Container Question

Slightly simplified example:

1
2
3
std::list< std::pair<int, int*> > example;
example.push_back(std::make_pair(1, NULL));
example.back().second = &example.back().first;


Would this be OK to do, assuming that example might have more elements added/removed from it? If it isn't, is there any container that does work like that? Or is this just a problem with my design?
I think as long as you are using std::list, this is safe. It does not reallocate the elements when you add or remove more elements in the container. But in general this is dangerous as container elements may change its location in the memory.
+1 krishnendu; Just looked it up:
23.2.2.3 list modifiers [lib.list.modifiers]
iterator insert(iterator position, const T& x);
void insert(iterator position, size_type n, const T& x);
template <class InputIterator>
void insert(iterator position, InputIterator first, InputIterator last);
void push_front(const T& x);
void push_back(const T& x);
1 Notes: Does not affect the validity of iterators and references. If an exception is thrown there are no effects.
...
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
void pop_front();
void pop_back();
void clear();
3 Effects: Invalidates only the iterators and references to the erased elements.

Regards
Alright, good. That's basically what I thought, but I just wanted to make sure.
Topic archived. No new replies allowed.