use of subscript([]) operator of map

  • Thread starter Krishanu Debnath
  • Start date
K

Krishanu Debnath

Hello,

In case the key is not present, map::eek:perator[] 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
 
G

Gianni Mariani

Krishanu said:
Hello,

In case the key is not present, map::eek:perator[] 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.
 
T

Tom Widmer

Hello,

In case the key is not present, map::eek:perator[] 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
 
A

Andrey Tarasevich

Krishanu said:
...
In case the key is not present, map::eek:perator[] 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<>::eek:perator []' is not a pure search
operation and is not intended to be used as such. 'std::map<>::eek:perator
[]' is a search-and-overwrite operation (hence no const version, BTW).
If you need a pure search operation, use 'std::map<>::find'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top