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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:20:18+00:00 2026-05-25T14:20:18+00:00

The Code: boost::unordered_map<int, boost::unordered_map<int, float>> map; { boost::unordered_map<int, float> h; h.insert(make_pair(1, 0.5)); map.insert(make_pair(5, h));

  • 0

The Code:

    boost::unordered_map<int, boost::unordered_map<int, float>> map;
{
    boost::unordered_map<int, float> h;
    h.insert(make_pair(1, 0.5));
    map.insert(make_pair(5, h));
}
{
    boost::unordered_map<int, float> h = map[5];
    h.insert(make_pair(2, 0.6));
    map.insert(make_pair(5, h));
}
cout << map[5].size() << endl;

Why the output is 1 not 2?
And when i use boost::unordered_map* > instead, then everything works well.
Can anyone help me?

  • 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-25T14:20:18+00:00Added an answer on May 25, 2026 at 2:20 pm

    Here’s what happens in the second block of code:

    1. h = map[5] creates a copy of the inner map.
    2. h.insert(...) adds a value to the copy of the inner map.
    3. map.insert(...) does nothing. unordered_map::insert inserts an element to the map if and only if there is no element in the map with an equivalent key. But the key 5 is already present, and so no insertion happens. You can confirm this by checking that the boolean portion of the return value from the insert call is false.

    At the end of the block, the copied map is discarded, and the original inner map, with a single value, remains in map. As a result, you get the output of map[5].size()==1.

    But why do we get an output of 2 if the value type of the inner map is set to a pointer, boost::unordered_map<int, float>*? The second block of code would do this:

    1. h = map[5] will get a pointer to the inner map inserted into map.
    2. h.insert(...) adds a value to the inner map directly — not into a copy. At this point, map[5].size()==2 already.
    3. map.insert(...) still does nothing. But there’s no need to — the inner map was already modified in place.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Boost.Serialization to serialize a std::map. The code looks like this void Dictionary::serialize(std::string
code: boost::filesystem::path config_folder(Config::CONFIG_FOLDER_NAME); if( !(boost::filesystem::exists(config_folder))) { std::cout << Network Config Directory not found...\n; std::cout
in my code i would like boost::shared_ptr not to call delete but call ptr->deleteMe()
I use the Boost Test framework for my C++ code but there are two
I use the Boost Test framework to unit test my C++ code and wondered
I am making extensive use of boost:shared_ptr in my code. In fact, most of
I make a lot of use of boost::scoped_ptr in my code and it is
I have the following code: #include <iostream> #include boost/unordered_map.hpp using namespace std; using namespace
In my project, I recently decided to use boost::thread. My code compiles fine under
the following code: /***************************************************************************/ boost::mutex m; struct func { func(int v):n(v) {} void operator()()

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.