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

  • Home
  • SEARCH
  • 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 9104097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:47:04+00:00 2026-06-17T01:47:04+00:00

Which way to assign values to a map is most efficient? Or are they

  • 0

Which way to assign values to a map is most efficient? Or are they all optimized to the same code (on most modern compilers)?

   // 1) Assignment using array index notation
   Foo["Bar"] = 12345;

   // 2) Assignment using member function insert() and STL pair
   Foo.insert(std::pair<string,int>("Bar", 12345));

   // 3) Assignment using member function insert() and "value_type()"
   Foo.insert(map<string,int>::value_type("Bar", 12345));

   // 4) Assignment using member function insert() and "make_pair()"
   Foo.insert(std::make_pair("Bar", 12345));

(I know I could benchmark and check compiler output, but this question arose now and the only thing I have close at hand is my mobile phone… hehe)

  • 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-17T01:47:05+00:00Added an answer on June 17, 2026 at 1:47 am

    First, there are semantic differences between [] and insert:

    • [] will replace the old value (if any)
    • insert will ignore the new value (if an old value is already present)

    therefore comparing the two is useless in general.

    Regarding the inserts versions:

    • std::map<std::string, int>::value_type is std::pair<std::string const, int> so no (important) difference between 3 and 4
    • std::make_pair("Bar", 12345) is cheaper than std::pair<std::string, int>("Bar", 12345) because the std::string type is a full fledged class with operations to do on copy and "Bar" is just a string literal (thus just a pointer copy); however since at the end you do need to create the std::string… it will depend on the quality of your compiler

    In general, I would recommend:

    • [] for updating
    • insert(std::make_pair()) for ignoring duplicates

    std::make_pair is not only shorter, it also respects the DRY guideline: Don’t Repeat Yourself.


    For completeness though, the fastest (and easiest) would be emplace (C++11 enabled):

    map.emplace("Bar", 12345);
    

    Its behavior is that of insert, but it constructs the new element in-place.

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

Sidebar

Related Questions

I have a table T (structure below) which initially contains all-NULL values in an
I'm trying to get make method in which I can assign values to multiple
I have to store integer values which are mapped to other integers. One way
Is there a way to assign a binary value to a VB variable? All
Which way is better for removing float decimals places or is there a more
In which way could I call the function that automatically opens my annotation (with
I was wondering which way is better to work with. Using private attributes at
I am wondering which way would end up being faster, selecting elements by: $('element[href=#my_link]');
I'm really interested which way you guys use to effectivly debug a large (let's
I don't know which way is better to use about uploading and saving a

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.