storing iterators

T

Thomas Grund

Hi,

If I store an iterator to a std::vector and then push_back some elements
then the iterator is not valid anymore since the internal data of the
vector is reallocated.

Can I store an iterator to a std::map?
So can I assume that the internal data of a map (or set) is not
reallocated?


#include <vector>
#include <map>
#include <iostream>

using namespace std;

int main()
{
// ----------- first case, this does not work
vector<int> v;
v.push_back(1);
vector<int>::iterator i = v.begin();

cout << *i << '\n';

v.push_back(2);
v.push_back(2);
v.push_back(2);

cout << *i << '\n'; // undefined, reallocation of internal data

// -----------second case works ?
map<int, int> m;
m[1] = 1;
map<int, int>::iterator j = m.find(1);

cout << j->second << '\n';

m[2] = 3;
m[3] = 2;
m[1] = 5;

cout << j->second << '\n';
// question: Is it sure that I get 5 here ?
}

If this works, can I also change values such as j->second = 10; ?


Thanks a lot!

Thomas
 
L

loufoque

Thomas said:
If I store an iterator to a std::vector and then push_back some elements
then the iterator is not valid anymore since the internal data of the
vector is reallocated.

Why not store the position instead since vectors give random access for
O(1) ?
 
T

Thomas Grund

Why not store the position instead since vectors give random access for O(1) ?

My question is concerning the map case. I want to build up some sparse
matrix structure. For this case I would like to have O(1) access to some
elements.
The example with the vector was just to tell that I am aware of possible
problems with using iterators.

Thomas
 
K

Kai-Uwe Bux

Thomas said:
Hi,

If I store an iterator to a std::vector and then push_back some elements
then the iterator is not valid anymore since the internal data of the
vector is reallocated.

Can I store an iterator to a std::map?
Yes.

So can I assume that the internal data of a map (or set) is not
reallocated?

Iterators and references are guaranteed to remain valid: erase-ops only
invalidate the iterators to the erased items; insert-ops do not invalidate
any iterators.
#include <vector>
#include <map>
#include <iostream>

using namespace std;

int main()
{
// ----------- first case, this does not work
vector<int> v;
v.push_back(1);
vector<int>::iterator i = v.begin();

cout << *i << '\n';

v.push_back(2);
v.push_back(2);
v.push_back(2);

cout << *i << '\n'; // undefined, reallocation of internal data

// -----------second case works ?
map<int, int> m;
m[1] = 1;
map<int, int>::iterator j = m.find(1);

cout << j->second << '\n';

m[2] = 3;
m[3] = 2;
m[1] = 5;

cout << j->second << '\n';
// question: Is it sure that I get 5 here ?
}
Yes.

If this works, can I also change values such as j->second = 10; ?

Yes.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top