By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

how does map iterator actually reference underlying object?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.