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

The Archive Base Latest Questions

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

There are two ways of map insertion: m[key] = val; Or m.insert(make_pair(key, val)); My

  • 0

There are two ways of map insertion:

m[key] = val;

Or

m.insert(make_pair(key, val));

My question is, which operation is faster?
People usually say the first one is slower, because the STL Standard at first ‘insert’ a default element if ‘key’ is not existing in map and then assign ‘val’ to the default element.

But I don’t see the second way is better because of ‘make_pair’. make_pair actually is a convenient way to make ‘pair’ compared to pair<T1, T2>(key, val). Anyway, both of them do two assignments, one is assigning ‘key’ to ‘pair.first’ and two is assigning ‘val’ to ‘pair.second’. After pair is made, map inserts the element initialized by ‘pair.second’.

So the first way is 1. ‘default construct of typeof(val)‘ 2. assignment
the second way is 1. assignment 2. ‘copy construct of typeof(val)‘

  • 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-21T09:12:14+00:00Added an answer on May 21, 2026 at 9:12 am

    Both accomplish different things.

    m[key] = val;
    

    Will insert a new key-value pair if the key doesn’t exist already, or it will overwrite the old value mapped to the key if it already exists.

    m.insert(make_pair(key, val));
    

    Will only insert the pair if key doesn’t exist yet, it will never overwrite the old value. So, choose accordingly to what you want to accomplish.
    For the question what is more efficient: profile. 😛 Probably the first way I’d say though. The assignment (aka copy) is the case for both ways, so the only difference lies in construction. As we all know and should implement, a default construction should basically be a no-op, and thus be very efficient. A copy is exactly that – a copy. So in way one we get a “no-op” and a copy, and in way two we get two copies.
    Edit: In the end, trust what your profiling tells you. My analysis was off like @Matthieu mentions in his comment, but that was my guessing. 🙂


    Then, we have C++0x coming, and the double-copy on the second way will be naught, as the pair can simply be moved now. So in the end, I think it falls back on my first point: Use the right way to accomplish the thing you want to do.

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

Sidebar

Related Questions

There are two ways in which I can easily make a key,value attribution in
Why are there two ways to declare constants in CPP? Which is better, or
In Scala there are two ways to define a method which takes no argument
I guess this question could be taken in two ways... (Generic) - is there
I find that there are two ways to set key in emacs: golbal-set-key and
There are two ways to overload operators for a C++ class: Inside class class
I found there are two ways to approach it, ReadDirectoryChangesW and FindFirstChangeNotification . I
In WPF, there are two ways to set the focus to an element. You
while creating a thread in java, there is two ways such as Extending threads
From previous post, I learnt that for there are two ways, at least, to

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.