Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: constant ve...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 45787 of 47031
Post > Topic >>

Re: constant versus mutable iterator types

by dizzy <dizzy@[EMAIL PROTECTED] > May 7, 2008 at 11:05 AM

subramanian100in@[EMAIL PROTECTED]
 India wrote:

> My understanding:
> Along the same above discussion, shouldn't map<Key, T>::iterator and
> multimap<Key, T>::iterator also be constant bidirectional iterators ?
> (This is what I expected because for both set, multiset, map,
> multimap, the key is constant)
> 
> However in this book, it is mentioned that for container map<Key, T>,
> map<Key, T>::iterator is mutable bidirectional iterator category and
> for container multimap<Key, T>, multimap<Key, T>::iterator is mutable
> bidirectional iterator category. Is this correct?
> 
> Is my understanding wrong ?
> 
> Kindly clarify.

To add some more info to what Ian and Greg already told you, each standard
container has a nested typedef named "value_type" which represents the
type
of the values stored in the container (and the iterator are just "like"
pointers to "value_type", so when you dereference an iterator you get
either a "value_type&" or a "value_type const&" depending if it is a
mutable iterator or not). 

std::set<Key> defines value_type to be Key so the fact that both iterator
and const_iterator operator* return "value_type const&" means both
iterator
types are not mutable. However, for std::map<Key, Value>, value_type is
std::pair<Key const, Value> (that's why you need all those "iter->second"
expressions to refer to the pointed to value) and
const_iterator::operator*
returns a "value_type const&" while iterator::operator* returns
a "value_type&" so technically the later is a mutable iterator while the
former is not. 

But, even so, you cannot change the Key pointed to by an mutable map
iterator because the mutable iterator will just give you access to
a "std::pair<Key const, Value>&" so you cannot modify the Key part of the
pair. Which means that while technically map<Key, Value>::iterator is a
mutable iterator, logically it is "semi-mutable" because it allows you
mutable access only to the value part.

-- 
Dizzy
 




 4 Posts in Topic:
constant versus mutable iterator types
"subramanian100in@[E  2008-05-06 20:51:52 
Re: constant versus mutable iterator types
Ian Collins <ian-news@  2008-05-07 16:12:17 
Re: constant versus mutable iterator types
Greg Herlihy <greghe@[  2008-05-06 22:17:27 
Re: constant versus mutable iterator types
dizzy <dizzy@[EMAIL PR  2008-05-07 11:05:10 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:43:51 CDT 2008.