473,320 Members | 1,900 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,320 software developers and data experts.

constant versus mutable iterator types

I am reading David Musser's "STL Tutorial and Reference Guide" Second
Edition.
In that book, on pages 68-69, definition has been given that "an
iterator can be mutable or constant depending on whether the result of
operator* is a reference or a constant reference."

As per this definition, on page 71 in this book, it is mentioned that
for 'set' and 'multiset', both the iterator and const_iterator types
are constant bidirectional types - in fact they are the same type.

The reason given in this book is as follows:

set<ints;
s.insert(3);
s.insert(5);
s.insert(7);
set<int>::iterator i = s.begin();
*i = 4; // incorrect

On page 72, it is given that for container set<T>, set<T>::iterator is
constant bidirectional iterator category and for container
multiset<T>, multiset<T>::iterator is constant bidirectional iterator
category.

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.

Thanks
V.Subramanian

Jun 27 '08 #1
2 4232
su**************@yahoo.com, India wrote:
I am reading David Musser's "STL Tutorial and Reference Guide" Second
Edition.
In that book, on pages 68-69, definition has been given that "an
iterator can be mutable or constant depending on whether the result of
operator* is a reference or a constant reference."

As per this definition, on page 71 in this book, it is mentioned that
for 'set' and 'multiset', both the iterator and const_iterator types
are constant bidirectional types - in fact they are the same type.

The reason given in this book is as follows:

set<ints;
s.insert(3);
s.insert(5);
s.insert(7);
set<int>::iterator i = s.begin();
*i = 4; // incorrect

On page 72, it is given that for container set<T>, set<T>::iterator is
constant bidirectional iterator category and for container
multiset<T>, multiset<T>::iterator is constant bidirectional iterator
category.

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)
No, the key may be constant, but the value is mutable.

--
Ian Collins.
Jun 27 '08 #2
su**************@yahoo.com, 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<Keydefines 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

Jun 27 '08 #3

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

Similar topics

9
by: Krisztian Kepes | last post by:
Hi ! I want to create an module and I want to use some Global Constant in it. How to I create an global constant in module what is accessable in from other modules ? like this example: ***...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
9
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences,...
2
by: Neal D. Becker | last post by:
x = for i in x: i = 2 This doesn't change x. 2 questions: 1) Why not? Why doesn't assign to an iterator of a mutable type change the underlying object?
25
by: tsaar2003 | last post by:
Hi Pythonians, To begin with I'd like to apologize that I am not very experienced Python programmer so please forgive me if the following text does not make any sense. I have been missing...
9
by: blangela | last post by:
Can somepoint me to a good discussion on the pros and cons of using #define versus a constant variable in your C+ application? Thanks, Bob
33
by: desktop | last post by:
In the C++ standard sec 23.1.2 table 69 it says that erase(q) where q is a pointer to an element can be done in amortized constant time. I guess that is not worst case since std::set is...
24
by: Steven D'Aprano | last post by:
Sometimes it seems that barely a day goes by without some newbie, or not- so-newbie, getting confused by the behaviour of functions with mutable default arguments. No sooner does one thread...
4
by: puzzlecracker | last post by:
How can I pass a reference to a method as constant? I tried the following: Function(const Foo f) or Function(readonly Foo f) Also, How to declare local variable to be constant const...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.