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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:48:34+00:00 2026-05-30T22:48:34+00:00

Suppose I have the following: std::map<KEY,VALUE> m1; std::map<KEY,VALUE> m2; What is the most direct

  • 0

Suppose I have the following:

std::map<KEY,VALUE> m1;
std::map<KEY,VALUE> m2;

What is the most direct way to move all key/value pairs from m1 into m2?

I would expect:

  • m1 to be empty after this operation
  • m2 may initially have pairs
  • those pairs in m2 that don’t have the same key as m1 should be left alone
  • those pairs in m2 that have the same key as m1 should be overwritten with m1’s pairs

Do I need a combination of calls from <algorithm>?

Solution

James Kranze’s solution satisfies my requirements.

for( const auto& p : m1 )
  m2[ p.first ] = p.second;
m1.clear();

Joachim Pileborg’s recommendation will only work if m2 and m1 do not have the same key (ie m2’s value will not be overwritten by m1’s value for the same key)

std::move( m1.begin(), m1.end(), std::inserter( m2, m2.begin() ));
  • 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-30T22:48:36+00:00Added an answer on May 30, 2026 at 10:48 pm

    The most obvious solution is just to write a loop yourself:

    for ( std::map<KEY, VALUE>::const_iterator current = m1.begin();
            current != m1.end();
            ++ current ) {
        m2[current->first] = current->second;
    }
    

    Otherwise, I think something like the following should work:

    std::copy( m2.begin(), m2.end(), std::inserter( m1, m1.end() ) );
    m2.clear();
    m2.swap( m1 );
    

    This isn’t exactly intuitive, and I’d hesitate to use it without
    comments, since:

    1. Since std::map doesn’t have push_back or push_front, you need
      to use the more general insterter, which in turn requires an iterator
      specifying where the insertion is to take place. Except that std::map
      treats this iterator as a “hint”, and since it generally won’t be a
      good hint, it will be ignored.

    2. You actually have to copy from m2 into m1, since insertion into a
      map will not overwrite any existing value, and when the key is present
      in both maps, you want to keep the value from m1.

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

Sidebar

Related Questions

Suppose I have: stl::map<std::string, Foo> myMap; is the following function thread safe? myMap[xyz] ?
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
The following code is supposed to find the key 3.0 in a std::map which
What a title, suppose I have a map like this: std::map<int, int> m; and
Suppose I have the following: std::string some_string = 2009-06-27 17:44:59.027; The question is: Give
Suppose I have following inheritance tree: SDLBullet inherits from Bullet inherits from Entity EnemyBullet
i am implementing dictionary in which key is a string keyword.suppose i have following
Suppose I have an std::istream pointing to the following contents (the line break is
Suppose I have the following function: void foo(std::vector<int> vec, int n); If I call
Suppose I have following string: String asd = this is test ass this is

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.