how does map iterator actually reference underlying object?

K

kjackson.ken

I have code such as the following:

map<int, string> stringMap;

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
 
C

Clark Cox

I have code such as the following:

map<int, string> stringMap;

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

James Kanze

I have code such as the following:
map<int, string> stringMap;
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top