Entretoize
Entretoize

Reputation: 2251

Keep address of inserted element in vector (c++)

Say I have a shared vector of int between several objects:

vector <int> list;

Each object can add more elements into it:

list.push_back(someint);

I would like these objects to be able to delete the elements them put inside, but I can't know the position because of the other object actions. Also if I keep the element address it will just correspond to the position in the list which will change so I don't know. Is it possible ?

Upvotes: 0

Views: 163

Answers (1)

Christian Hackl
Christian Hackl

Reputation: 27528

So you have a std::vector and you add an element. Then you erase and add one or more other elements, and finally you need to erase the original element?

std::vector is not really suited for that. Why not use a std::list instead? With a std::list, you can insert an element and will get an iterator to the newly inserted element. Unlike with std::vector, a std::list iterator will stay valid even if other elements are later added or removed.

Upvotes: 2

Related Questions