473,491 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how does map iterator actually reference underlying object?

I have code such as the following:

map<int, stringstringMap;

stringMap.insert(map<int,string>::value_type(1, "hello"));

map<int, string>::iterator iter;

iter = stringMap.find(1);

iter->second = "goodbye";

I know after experimentation that when I use the iterator to change
the value of second, that I am actually manipulating the underlying
object. So the next time I call find, it will reference "goodbye".

My question is, how does it accomplish this? Is this a hard and fast
rule? Can I assume that the find member function for maps will not
copy-by-value and always allow me to manipulate the underlying object
through the interator, even if it were things like pointers, etc?

Thanks,

Ken Jackson

Jun 10 '07 #1
2 3793
On 2007-06-10 11:41:24 -0700, kj**********@gmail.com said:
I have code such as the following:

map<int, stringstringMap;

stringMap.insert(map<int,string>::value_type(1, "hello"));

map<int, string>::iterator iter;

iter = stringMap.find(1);

iter->second = "goodbye";

I know after experimentation that when I use the iterator to change
the value of second, that I am actually manipulating the underlying
object. So the next time I call find, it will reference "goodbye".

My question is, how does it accomplish this?
By implementing operator-to return a pointer to the actual pair
contained in the map.
Is this a hard and fast
rule? Can I assume that the find member function for maps will not
copy-by-value and always allow me to manipulate the underlying object
through the interator, even if it were things like pointers, etc?
Yes, you can rely on this. Just as you can for any of the standard
container's non constant iterators.
--
Clark S. Cox III
cl*******@gmail.com

Jun 10 '07 #2
On Jun 10, 8:41 pm, kjackson....@gmail.com wrote:
I have code such as the following:
map<int, stringstringMap;
stringMap.insert(map<int,string>::value_type(1, "hello"));
map<int, string>::iterator iter;
iter = stringMap.find(1);
iter->second = "goodbye";
I know after experimentation that when I use the iterator to change
the value of second, that I am actually manipulating the underlying
object. So the next time I call find, it will reference "goodbye".
Correct.
My question is, how does it accomplish this?
By having the -return a reference to the underlying data
structure.
Is this a hard and fast rule?
For the standard containers, yes. In fact, the requirements of
iterators require this for all iterators except input and output
iterators.
Can I assume that the find member function for maps will not
copy-by-value and always allow me to manipulate the underlying object
through the interator, even if it were things like pointers, etc?
The find member function returns an iterator, and *that* is
returned by value. But the dereference operators of the
iterator (* and ->) are required to return references to the
actual data element in the container. For all standard
containers.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 10 '07 #3

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

Similar topics

7
3251
by: Rene Pijlman | last post by:
Section 6.5 "What is delegation?" of the FAQ says: "Python programmers can easily implement delegation. For example, the following class implements a class that behaves like a file but converts...
5
2062
by: Leif K-Brooks | last post by:
Is there a word for an iterable object which isn't also an iterator, and therefor can be iterated over multiple times without being exhausted? "Sequence" is close, but a non-iterator iterable could...
0
3380
by: sks_cpp | last post by:
I am trying to wrap the map iterator for keys and values. However, I seem to run into problems with the values for const_iterator - it works for all other combinations. Below I list my code and...
20
3192
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external...
5
2076
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the...
16
2551
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
3
2786
by: Belebele | last post by:
I have an Element class which is abstract and I would like to have an object of the Iterator class to iterate over a range of elements. I would like to use std::for_each to instrument the...
7
3812
by: Max Odendahl | last post by:
Hi, my own declared class has a stl::list as a member variable. I now need access to those from the outside. Is a custom iterator for my class the best solution for this and how to do this? I...
2
13035
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
0
7112
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
6974
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
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6852
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...
1
4878
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...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.