473,396 Members | 2,061 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,396 software developers and data experts.

need for operator[] in map

For inserting new elements in map, we can use insert member function.

To know if an element exists or not in a map, we can use count or find
member function.

Also, we can use the iterator returned by find to modify the mapped
value of an existing key.

When we use operator[], it may add an element into the map if the key
already doesn't exist, which may not be always wanted.

Given this, I am unable to understand the reason as to why we have
operator[] in map whose functionality can be achieved by other member
functions.

Kindly clarify.

Thanks
V.Subramanian
Feb 7 '08 #1
5 1427
On Feb 7, 2:56 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
For inserting new elements in map, we can use insert member function.

To know if an element exists or not in a map, we can use count or find
member function.

Also, we can use the iterator returned by find to modify the mapped
value of an existing key.

When we use operator[], it may add an element into the map if the key
already doesn't exist, which may not be always wanted.

Given this, I am unable to understand the reason as to why we have
operator[] in map whose functionality can be achieved by other member
functions.

Kindly clarify.

Thanks
V.Subramanian
You've already got a response on your original post - if that doesn't
answer the question, reply and give some information as to how it
didn't help.
Don't just post the same question again, especially on the same day.

Rob.
Feb 7 '08 #2
On Feb 7, 10:00 am, "[rob desbois]" <rob.desb...@gmail.comwrote:
On Feb 7, 2:56 pm, "subramanian10...@yahoo.com, India"

<subramanian10...@yahoo.comwrote:
For inserting new elements in map, we can use insert member function.
To know if an element exists or not in a map, we can use count or find
member function.
Also, we can use the iterator returned by find to modify the mapped
value of an existing key.
When we use operator[], it may add an element into the map if the key
already doesn't exist, which may not be always wanted.
Given this, I am unable to understand the reason as to why we have
operator[] in map whose functionality can be achieved by other member
functions.
Kindly clarify.
Thanks
V.Subramanian

You've already got a response on your original post - if that doesn't
answer the question, reply and give some information as to how it
didn't help.
Don't just post the same question again, especially on the same day.

Rob.
If you are referring to the other thread with the subject
"using operator[] or insert in map ?",
then I would like to say that I got clarified for THAT doubt.

If you are referring to some other thread, please give the subject of
the thread you are referring to.

In this particular thread ie the thread with the subject
"need for operator[] in map",
I want to know the reason for having operator[] in map.

Since the modification of mapped-value for a given key can be done
through dereferencing and modifying the iterator returned by find()
operation, I - as a beginner in STL, am unable to understand the
reason for having operator[] in map.

I still think the question I have posted here is different from the
other thread with the subject: "using operator[] or insert in map ?"

Please provide me an answer for this thread.

Thanks
V.Subramanian
Feb 7 '08 #3
On Feb 7, 2:56*pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
For inserting new elements in map, we can use insert member function.

To know if an element exists or not in a map, we can use count or find
member function.

Also, we can use the iterator returned by find to modify the mapped
value of an existing key.

When we use operator[], it may add an element into the map if the key
already doesn't exist, which may not be always wanted.

Given this, I am unable to understand the reason as to why we have
operator[] in map whose functionality can be achieved by other member
functions.
My guess is: syntactic elegance, convenience,
familiarity, symmetry. IOW it's nice to be
able to be able to do:

foomap["bar"] = "baz";
// then
s = foomap["bar"];

You only need to look at code using [] vs
the alternatives to see this for yourself.

Feb 7 '08 #4
On Feb 7, 3:56 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
For inserting new elements in map, we can use insert member function.
To know if an element exists or not in a map, we can use count or find
member function.
Also, we can use the iterator returned by find to modify the mapped
value of an existing key.
When we use operator[], it may add an element into the map if the key
already doesn't exist, which may not be always wanted.
Given this, I am unable to understand the reason as to why we have
operator[] in map whose functionality can be achieved by other member
functions.
Convenience. It's a bit awkward, in that the semantics which
would be most convenient vary according to what you're using the
map for, and in practice, I rarely use []. (But then, most of
my maps are const, so I can't.) The real question is, of
course, if [] doesn't find the element, what does it do?

In the most general case, because of such uncertainty surronding
the semantics, one could argue that map shouldn't support an
operator[] at all---that its a case of operator overloading
abuse. In practice, however, smaller, text based languages like
AWK and perl use it effectively, and it seems reasonable to
support it in the same way they do---if you are using std::map
like you'd use an array in AWK or perl, you use it (and not much
else of the interface of std::map); if you are using std::map in
some other context, you don't use it.

--
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
Feb 8 '08 #5
<su**************@yahoo.com wrote:
For inserting new elements in map, we can use insert member function.

To know if an element exists or not in a map, we can use count or find
member function.

Also, we can use the iterator returned by find to modify the mapped
value of an existing key.

When we use operator[], it may add an element into the map if the key
already doesn't exist, which may not be always wanted.

Given this, I am unable to understand the reason as to why we have
operator[] in map whose functionality can be achieved by other member
functions.

Kindly clarify.
For the answer, you will have to ask the standards body, or at least
Alex Stepanov (the creator of the STL.)

For now, maybe the explanation from SGI's website will do?

Note that the definition of operator[] is extremely simple: m[k] is
equivalent to (*((m.insert(value_type(k,
data_type()))).first)).second. Strictly speaking, this member
function is unnecessary: it exists only for convenience.
(http://www.sgi.com/tech/stl/Map.html)
Feb 8 '08 #6

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

Similar topics

5
by: Carl Bevil | last post by:
I'm creating a library for internal use that relies on third-party code. I don't want clients of this library to know anything about the third party code, when compiling or running. Generally...
1
by: John Cho | last post by:
if i do a friend Testclass operator +(Test class &obj2); it is not correct because i need two objects?
13
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
4
by: dinks | last post by:
Hi I'm really new to c++ so please forgive me if this is really basic but im stuck... I am trying to make a data class that uses istringstram and overloaded << and >> operators to input and output...
1
by: akickdoe22 | last post by:
Please help me finish this program. i have completed the addition and the subtraction parts, but i am stuck on the multiplication and division. any suggestions, hints, code, anyhting. it's not a...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
6
by: naknak | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
3
by: Diwa | last post by:
In TC++PL 3edn, in section 21.2.2, Stroustrup notes that put( ) and write( ) simply write chars. Therefore, << for outputting chars need not be a member What does he mean here ? Also,...
1
by: anumliaqat | last post by:
hello!!! I am new to this site.can anyone help me in my program.It is of rational number class in c++ having operator overloading.It is not executing and i am not able to find mistakes. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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...

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.