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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:24:30+00:00 2026-06-01T20:24:30+00:00

when I have a std::map, is there an elegant way to at the same

  • 0

when I have a std::map, is there an elegant way to at the same time:

  1. insert / edit an element given its key
  2. get an iterator to the inserted element

The best way I found that prevent doing 2 look-up in the map is:

std::map<int, int> myMap;
//do some stuff with the map
std::map<int,int>::iterator  it = myMap.insert(std::pair<int, int>(0,0)).first;
it->second = 0; //necessary because insert does not overwrite the values

Is it possible to do both of those things in a single statement / line?
thanks

  • 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-01T20:24:31+00:00Added an answer on June 1, 2026 at 8:24 pm

    Alas, the STL functions and containers don’t always do what you’d expect. Here are two generic versions, the first more like your code above:

    template<class Map>
    inline typename Map::iterator ForceInsert1( 
        Map&                           m, 
        const typename Map::key_type&  k, 
        const typename Map::data_type& d )
    {
        typename Map::iterator it = m.insert( 
            typename Map::value_type( k, d ) ).first;
        it->second = d; // only necessary if the key already exists
        return it;
    }
    
    template<class Map>
    inline typename Map::iterator ForceInsert2( 
        Map&                           m, 
        const typename Map::key_type&  k, 
        const typename Map::data_type& d )
    {
        typename Map::iterator it = m.find( k );
        if( it != m.end() )
        {
            it->second = d;
        }
        else
        {
            it = m.insert( typename Map::value_type( k, d ) ).first;
        }
        return it;
    }
    
    typedef std::map<int, int> MyMap;
    void Foo( MyMap& myMap )
    {
        ForceInsert1( myMap, 42, 100 );
        ForceInsert2( myMap, 64, 128 );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

std::map<any, string> is not working so I wonder if there's another approach to have
i have a std::map and i am using iterator to find a certain key,value
I have such a map: std::map<time_t, int> There is one value (int) per day
I have a symbol table implemented as a std::map . For the value, there
I just found std::map.insert could accept an iterator as its first parameter as a
I have a settings which are stored in std::map . For example, there is
Is there an stl way to get a list of values from a map?
Currently, I have a std::map <DWORD, DWORD> table and I'm looking for a key
unordered_map<std::string, std::string>* Accounts; I have this code up there to initialize from a pointer,
let's say I have std::map< std::string, std::string > m_someMap as a private member variable

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.