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>::itera tor i = s.begin();
*i = 4; // incorrect
On page 72, it is given that for container set<T>, set<T>::iterato r is
constant bidirectional iterator category and for container
multiset<T>, multiset<T>::it erator 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 2 4270 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>::itera tor i = s.begin();
*i = 4; // incorrect
On page 72, it is given that for container set<T>, set<T>::iterato r is
constant bidirectional iterator category and for container
multiset<T>, multiset<T>::it erator 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. 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<Keydef ines 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::opera tor* 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>::iterato r is a
mutable iterator, logically it is "semi-mutable" because it allows you
mutable access only to the value part.
--
Dizzy This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
*** module dirs ***
const Const_Up=1
const Const_Down=1
|
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 interchangeable in all
circumstances (as long as they're paired) so there's no overloading to
muddy the language. Of course there could be some interesting problems
with current code that doesn't make a distinction, but it would be dead
easy to fix...
|
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, but try to use
an iterator of a list that you're mutating and watch it all fall to
pieces. That is, if you change the length of a section of the list
through which the iterator has already passed, it will lose track of
where it is. I think...
|
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?
|
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 constants in Python language. There are some
workarounds available, for example the const-module. To me, this looks
quite cumbersome and very unintuitive. For the interpreter, in the
efficiency-wise, I just cannot tell.
| |
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
|
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 practically a red-black
tree where insert/delete takes O(lg n) time. Or are there some other
explanation for this complexity?
|
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 finally, and painfully, fade
away than another one starts up.
I suggest that Python should raise warnings.RuntimeWarning (or similar?)
when a function is defined with a default argument consisting of a list,
dict or set. (This is not meant as an...
|
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 Foo foo or readonlyFoo f?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |