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 9299499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:34:30+00:00 2026-06-18T22:34:30+00:00

I have an std::unordered_map , and I want both to increment the first value

  • 0

I have an std::unordered_map, and I want both to increment the first value in a std::pair, hashed by key, and to create a reference to key. For example:

std::unordered_map<int, std::pair<int, int> > hash;
hash[key].first++;

auto it(hash.find(key));
int& my_ref(it->first);

I could, instead of using the [] operator, insert the data with insert(), but I’d allocate a pair, even if it were to be deallocated later, as hash may already have key — not sure of it, though. Making it clearer:

// If "key" is already inserted, the pair(s) will be allocated
// and then deallocated, right?
auto it(hash.insert(std::make_pair(key, std::make_pair(0, 0))));
it->second.first++;

// Here I can have my reference, with extra memory operations,
// but without an extra search in `hash`
int& my_ref(it->first);

I’m pretty much inclined to use the first option, but I can’t seem to decide which one is the best. Any better solution to this?

P.S.: an ideal solution for me would be something like an insertion that does not require an initial, possibly useless, allocation of the 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-06-18T22:34:31+00:00Added an answer on June 18, 2026 at 10:34 pm

    As others have pointed out, a “allocating” a std::pair<int,int> is really nothing more than copying two integers (on the stack). For the map<int,pair<int,int>>::value_type, which is pair<int const, pair<int, int>> you are at three ints, so there is no significant overhead in using your second approach. You can slightly optimize by using emplace instead of insert i.e.:

    // Here an `int` and a struct containing two `int`s are passed as arguments (by value)
    auto it(hash.emplace(key, std::make_pair(0, 0)).first);
    it->second.first++;
    
    // You get your reference, without an extra search in `hash`
    // Not sure what "extra memory operations" you worry about
    int const& my_ref(it->first); 
    

    Your first approach, using both hash[key] and hash.find(key) is bound to be more expensive, because an element search will certainly be more expensive than an iterator dereference.

    Premature copying of arguments on their way to construction of the unordered_map<...>::value_type is a negligible problem, when all arguments are just ints. But if instead you have a heavyweight key_type or a pair of heavyweight types as mapped_type, you can use the following variant of the above to forward everything by reference as far as possible (and use move semantics for rvalues):

    // Here key and arguments to construct mapped_type 
    // are forwarded as tuples of universal references
    // There is no copying of key or value nor construction of a pair 
    // unless a new map element is needed.
    auto it(hash.emplace(std::piecewise_construct, 
                            std::forward_as_tuple(key), // one-element tuple
                            std::forward_as_tuple(0, 0) // args to construct mapped_type
                         ).first);
    it->second.first++;
    
    // As in all solutions, get your reference from the iterator we already have
    int const& my_ref(it->first); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a static global std::unordered_map in the cpp of my entry
I have an object defined as : std::unordered_map<std::string, std::vector<int>> large_obj; It can store very
If two keys for a std::unordered_map have the same hash value, is it guaranteed
To support user-defined key types in std::unordered_set<Key> and std::unordered_map<Key, Value> one has to provide
I have the following code: boost::unordered_map<std::string, int> map; map[hello]++; map[world]++; for(boost::unordered_map<std::string, int>::iterator it =
I'm using an std::unordered_map<key,value> in my implementation. i will be using any of the
I'm trying to use a std::pair of enum values as key for an unordered_map
unordered_map<std::string,unordered_map<std::string, std::string> >* storing_vars; I have this variable in the scope declared in the
I Have a object which the following field : boost::unordered_map<std::string, std::shared_ptr<Foo> > m_liste_; I
unordered_map<std::string, std::string>* Accounts; I have this code up there to initialize from a pointer,

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.