473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

storing iterators


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<intv;
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, intm;
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
Nov 18 '06 #1
4 4439
Thomas Grund wrote:
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) ?
Nov 18 '06 #2
On Sat, 18 Nov 2006, loufoque wrote:
Thomas Grund wrote:
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) ?
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
Nov 18 '06 #3
Thomas Grund wrote:
>
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<intv;
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, intm;
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

Nov 18 '06 #4


(...)

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

Thanks!

Thomas

Nov 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
18
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
2
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
0
by: toton | last post by:
Hi, I have a little design related problem, and finding a good way to resolve it. To state the problem, I have points from writing of several documents. Each document represents a Session class ,...
5
by: toton | last post by:
Hi, I have a deque of Point class,. Point class have two fields x, and y. Now I want another class Character should point to a portion of the deque, and allow iteration only on the portion. Thus...
13
by: r.z. | last post by:
I logged construtor and destructor calls for one of my classes and I discovered that the constructor was called only once while the destructor was called 3 times for a single object. I have a...
18
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
4
by: alexjcollins | last post by:
The following program demonstrates the problem: #include <vector> #include <iostream> #include <tvmet/Vector.h> typedef tvmet::Vector<double, 3Vector3d; class Mesh {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.