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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:46:06+00:00 2026-06-12T22:46:06+00:00

I have 3 maps: map<string, vector<int> > map1 map<string, vector<int> > map2 map<string, vector<int>

  • 0

I have 3 maps:

map<string, vector<int> > map1
map<string, vector<int> > map2
map<string, vector<int> > map3

I need to create a third map with all the strings existing in both map1 and map2 with their corresponding vectors. Thing is, even if the strings are same, their vectors can be different and i have to append all the vectors from both the common strings into 1 vector.
This is what I’m trying but I’m kind of lost:

for(map<string, vector<int> >::iterator it1=map1.begin(); it1 != map1.end(); it1++)
{
    string name = (*it1).first;
    for(map<string, vector<int> >::iterator it2=map2.begin(); it2 != map2.end(); it2++)
    {
        string name2 = (*it2).first;
        if (name == name2)
        {
            map3[name2] = (*it2).second;

        }
    }
}

Thanks so much for your help!!

  • 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-12T22:46:09+00:00Added an answer on June 12, 2026 at 10:46 pm

    Since the maps are ordered you can do this in linear time. This is one possible way to do the task:

    #include <map>
    #include <string>
    #include <vector>
    
    typedef std::vector<int> vec_t;
    typedef std::map<std::string, vec_t> map_t;
    typedef map_t::const_iterator iter_t;
    
    int main()
    {
        map_t map1, map2, map3;
    
        iter_t it1 = map1.begin();
        iter_t it2 = map2.begin();
    
        while (it1 != map1.end() && it2 != map2.end()) {
            if (it1->first < it2->first) {
                ++it1;
            } else if (it2->first < it1->first) {
                ++it2;
            } else { // equal keys
                vec_t temp(it1->second);
                temp.insert(temp.end(), it2->second.begin(), it2->second.end());
                map3[it1->first].swap(temp); // swap contents to new map entry
                ++it1;
                ++it2;
            }
        }
    }
    

    Note that this assumes the map key ordering is the default less<Key>.

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

Sidebar

Related Questions

I have many maps like Map(String, Object) . map1 map2 .... mapN I need
I have two maps: Map<String, Object> map1; Map<String, Object> map2; I need to receive
Let's say I have two maps: typedef int Id; std::map<Id, std::string> idToStringMap; std::map<Id, double>
I have a function which creates an array of Maps: map<string, int> *pMap And
I have a vector of maps: typedef map<string, string> aMap; typedef vector<aMap> rVec; rVec
I find myself using a lot of nested maps, e.g a Map[Int, Map[String, Set[String]]],
I have collection of maps. Collection<Map<String,Object>> xyz = (Collection<Map<String,Object>>) someMethod(); xyz.add(new HashMap<>()); If I
I have two maps, tk1 and tk2 with the following structure: std::map<std::string, double>tk1; std::map<std::string,
I need to have a resultset converted into a Map of Maps. The reason
I have two Maps that contain the same type of Objects: Map<String, TaskJSO> a

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.