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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:48:03+00:00 2026-06-11T21:48:03+00:00

If I have two maps which are guaranteed to have exactly the same set

  • 0

If I have two maps which are guaranteed to have exactly the same set of keys, how can I efficiently iterate through both maps?

For example, say I have the following maps:

std::map<std::string, int> iMap;
std::map<std::string, std::vector<int> > vMap;

At some point they both end up with exactly the same set of keys. I now need to update all values of vMap based on the corresponding iMap value. The first thing that comes to mind is something this:

typedef map<string, int> map_t;
BOOST_FOREACH(map_t::value_type &p, iMap) {
  vMap[p.first].push_back(p.second);
}

However, it seems rather wasteful that we have to lookup each value of vMap[n] considering we’re effectively going through the keys in order. Is there any way we can take advantage of this?

  • 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-11T21:48:05+00:00Added an answer on June 11, 2026 at 9:48 pm

    If you’re absolutely sure that the keys are identical, you can iterate over both maps in lockstep:

    auto it1 = iMap.begin();
    auto it2 = vMap.begin();
    
    while (it1 != iMap.end())
    {
        it2->second.push_back(*it1);
        ++it1;
        ++it2;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script which maps two drives, and then proceeds to copy
I would like to have a mapping which maps two string into one string.
In my current C++-project I have an STL map which maps integer keys onto
I have two Maps that contain the same type of Objects: Map<String, TaskJSO> a
I have two maps of type Map<Long, Integer>, one named oldValues representing the old
Let's say I have two maps: typedef int Id; std::map<Id, std::string> idToStringMap; std::map<Id, double>
I have a requirement to create two different maps in C++. The Key is
I have two classes (MVC view model) which inherits from one abstract base class.
I am planning to enlarge my application funcionality with maps. I have seen two
I have a MapPolygon which covers a certain area on the Silverlight Bing Maps

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.