Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6199709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:14:12+00:00 2026-05-24T04:14:12+00:00

Firstly could someone clarify whether in C++ the use of the [] operator in

  • 0

Firstly could someone clarify whether in C++ the use of the [] operator in conjunction with an unordered_map for lookups wraps a call to the find() method, or is using the [] operator quicker than find()?

Secondly, in the following piece of code I suspect in cases where the key is not already in the unordered_map I am performing a second look up by way of the line map[key] = value in order to replace the default value created there by using the [] operator when a key is not present.

Is that true, and if so is there a way (perhaps by use of pointers or something) that I might only perform one look up in any case (perhaps by storing the address of where to place a value/read a value from) and still achieve the same functionality? Obviously this would be a useful efficiency improvement if so.

Here is the modified code excerpt:

    int stored_val = map[key]; // first look up. Does this wrap ->find()??

    // return the corresponding value if we find the key in the map - ie != 0
    if (stored_val) return stored_val;

    // if not in map
    map[key] = value; 
       /* second (unnecessary?) look up here to find position for newly 
          added key entry */

   return value;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:14:13+00:00Added an answer on May 24, 2026 at 4:14 am

    operator[] will insert an entry for you with a default-constructed value, if one isn’t already there. It is equivalent to, but will probably be implemented more efficiently than:

    iterator iter = map.find(key);
    
    if(iter == map.end())
    {
        iter = map.insert(value_type(key, int())).first;
    }
    
    return *iter;
    

    operator[] can be quicker than doing the work manually with find() and insert(), because it can save having to re-hash the key.

    One way you can work around having multiple lookups in your code is to take a reference to the value:

    int &stored_val = map[key];
    
    // return the corresponding value if we find the key in the map - ie != 0
    if (stored_val) return stored_val;
    
    // if not in map
    stored_val = value;
    
    return value;
    

    Note that if the value doesn’t exist in the map, operator[] will default-construct and insert one. So while this will avoid multiple lookups, it might actually be slower if used with a type that is slower to default-construct + assign than to copy- or move-construct.

    With int though, which cheaply default-constructs to 0, you might be able to treat 0 as a magic number meaning empty. This looks like it might be the case in your example.

    If you have no such magic number, you’ve got two options. What you should use depends on how expensive it is for you to compute the value.

    First, when hashing the key is cheap but computing the value is expensive, find() may be the best option. This will hash twice but only compute the value when needed:

    iterator iter = map.find(key);
    
    // return the corresponding value if we find the key in the map
    if(iter != map.end()) return *iter;
    
    // if not in map
    map.insert(value_type(key, value));
    
    return value;
    

    But if you’ve got the value already, you can do it very efficiently — perhaps slighty more efficiently than using a reference + magic number as above:

    pair<iterator,bool> iter = map.insert(value_type(key, value));
    return *iter.first;
    

    If the bool returned by map.insert(value_type) is true, the item was inserted. Otherwise, it already existed and no modifications were made. The iterator returned points to the inserted or existing value in the map. For your simple example, this may be the best option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please show how i could parse a ics file using NSScanner? (Iphone
Firstly I am assuming DependencyProperty is what to use but I could be wrong
Firstly could someone please be kind enough to comment on why Microsoft might have
Firstly could you view my outcome http://jsfiddle.net/RXnKR/ From this I decided as you click
I'm trying to binarise a picture, firstly of course having it prepared(grayscaling) My method
Could someone please give me some advice/ideas about how to deal with the situations
Firstly I appreciate that this question could be seen as subjective but I strongly
Programming languages seem to go through several stages. Firstly, someone dreams up a new
I would really appreciate it if someone could point me towards beginning with FFMpeg.
Could you please explain me the logic of UNIX signal system: firstly it sends

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.