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

use of subscript([]) operator of map

Hello,

In case the key is not present, map::operator[] returns(and also insert)
a pair with that key and a default value.
Now my question is whether addition of these extra 'unwanted
key' can increase the run time for search operation. I am not sure about
the implementation.

Thanks
Krishanu


Jul 22 '05 #1
3 2376
Krishanu Debnath wrote:
Hello,

In case the key is not present, map::operator[] returns(and also insert)
a pair with that key and a default value.
Now my question is whether addition of these extra 'unwanted
key' can increase the run time for search operation. I am not sure about
the implementation.


Absolutely, but if it is significant is the real question. This depends
on just how many you're inserting and just how sensitive your
application is to that and we have no way of knowing.

Jul 22 '05 #2
On Wed, 08 Dec 2004 20:33:57 +0530, Krishanu Debnath
<in*****@domian.com> wrote:
Hello,

In case the key is not present, map::operator[] returns(and also insert)
a pair with that key and a default value.
Now my question is whether addition of these extra 'unwanted
key' can increase the run time for search operation. I am not sure about
the implementation.


Yes. Lookup is O(log m.size()), so adding elements slows down lookup.
If you just want to lookup an element then use map::find.

Tom
Jul 22 '05 #3
Krishanu Debnath wrote:
...
In case the key is not present, map::operator[] returns(and also insert)
a pair with that key and a default value.
Now my question is whether addition of these extra 'unwanted
key' can increase the run time for search operation. I am not sure about
the implementation.
...


Well, it probably does increase the run time. However, I don't exactly
understand your concern. 'std::map<>::operator []' is not a pure search
operation and is not intended to be used as such. 'std::map<>::operator
[]' is a search-and-overwrite operation (hence no const version, BTW).
If you need a pure search operation, use 'std::map<>::find'.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #4

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

Similar topics

15
by: Steve | last post by:
Hi, I hope someone can help. I have a class called cField, and another class called cFieldList. cFieldList contains a std::vector of cFields called myvec I've overloaded the subscript...
5
by: Steve | last post by:
Hi, I have a class called cList as so: template<class T> class cList { // base class for Lists private: protected: vector<T> tListOf; // field list container public: void Add(const T& t)...
10
by: olson_ord | last post by:
Hi, I am not exactly new to C++, but I have never done operator overloading before. I have some old code that tries to implement a Shift Register - but I cannot seem to get it to work. Here's a...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
5
by: DaVinci | last post by:
/* how to overload the operation ,subscipt of 2D-array? * how can I get element through piece,rather than piece(i)? * */ #include"global.h" using namespace std; class Piece { public: Piece()...
3
by: murali | last post by:
hello everybody... how can i overload the subscript operator with more than one dimension... like .... if possible please give an example... thank you...
6
by: josh | last post by:
Hi I've a dubt! when we have overloaded functions the compiler chooses the right being based on the argument lists...but when we have two subscript overloaded functions it resolves them being...
4
by: gvr123 | last post by:
Hi all This seems to me a peculiar problem, but confounding nonetheless... The problem seems to be that an overloaded subscript operator isn't being called unless it is called explicitly ...
2
by: subramanian100in | last post by:
For vector and deque, the 'at( )' member function throws out_of_range exception if the argument to the 'at( )' function is not in range. But the subscript operator does not throw this exception...
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
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...
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.