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
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