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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:04:45+00:00 2026-06-11T10:04:45+00:00

map<pair<double,double>,double> vectorDoubleMap; vectorDoubleMap[ pair<double, double>(10, 10) ] = 1; //1. vectorDoubleMap.insert( pair<double, double>(10, 10),

  • 0
map<pair<double,double>,double> vectorDoubleMap;

vectorDoubleMap[ pair<double, double>(10, 10) ] = 1;          //1.
vectorDoubleMap.insert( pair<double, double>(10, 10), 1);     //2.

‘1.’ statement compile done, but ‘2.’ statement not compiled.

What’s the difference between these two statement?

  • 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-11T10:04:46+00:00Added an answer on June 11, 2026 at 10:04 am

    map::insert takes a single pair argument holding key and value, not key and value as separate arguments. If you pass a second argument, the first is an iterator used as a hint for alikely insertion position, whereas the second is still a pair.

    So you’d have to write one of these:

    vectorDoubleMap.insert(pair<pair<double, double>, double>(pairs<double, double>(10, 10), 1));
    vectorDoubleMap.insert(make_pair(make_pair(10, 10), 1));
    

    You can see how make_pair makes the syntax much more concise in this case. (As the numbers are entered as integer literals, integer pairs will be constructed at first, but there is a conversion constructor for pairs of one kind to another, so this is all right and will be converted to double as needed. The compiler might even optimize the integers away and directly use doubles in the code it generates.)

    If the key is not present in the map, the two statements will be equal in effect and similar in performance. If the key is already present, the […]=… form will overwrite it, whereas the insert(…) form will keep the old value.

    Note that C++17 will likely offer two more ways to accomplish the same thing. insert_or_assign is roughly equivalent to your first line, since it will overwrite existing values. try_emplace is more like the second, since it won’t overwrite if the key is already present. The key benefit for both of these is that they accept arguments for a constructor, and construct the object in place. Which isn’t of much use for double, but might be useful for other objects which are hard to construct, copy or move. But still, the fact that try_emplace takes separate arguments and not a single pair makes that one a nice replacement for the insert discussed above.

    vectorDoubleMap.insert_or_assign(make_pair(10, 10), 1);
    vectorDoubleMap.try_emplace(make_pair(10, 10), 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two boost::fusion maps. namespace bf = boost::fusion; typedef bf::map<bf::pair<char,float>,bf::pair<int,double>> FusionMap;
I wrote this little code std::map<int,template<class T>> map_; map_.insert(make_pair<int,message>(myMsg.id,myMsg)); but the compiler doesn't seem
map<int, BasicBlock*> basicBlocks; // in my header file basicBlocks.insert(std::pair<int, BasicBlock*>(pc, bb)); where pc is
I'm trying to insert some pair value into a map. May map is composed
I want to insert a pair< string, vector<float> > into a map, first it
I have a situation where I want to map a pair of objects to
Does anyone experience to get depth map from stereo pair? I wonder if are
How to modify the vlaue of a key-value Pair from a map while I
map.root :controller => main, :action => index is not redirecting the main controller to
I have got a std::list< std::pair<std::string,double> > , which I know is sorted according

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.