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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:47:38+00:00 2026-05-27T05:47:38+00:00

I have a map which contains integer values. i want to re-arange this map

  • 0

I have a map which contains integer values. i want to re-arange this map into a vector of vector such that, all the common elements are inside a one vector. so, i have implemented the following code.
but the problem is my map contains huge list of data in both direction.so, i am worring my method is slow as i am always erasing the elements of my map. So, i want to improve this method. do you think the erasing the element is the best way. if you know, give me some other efficient way. if you think my method can still improve please ammend my code. i have given a sample data how my data look likes to get you idea.
i want this vector of vector to put a unique label for each common elements.
thank you in advance.

//populate my map from the upper part of my program

vector<int> list;
vector<vector<int> > listoflist;
map<int,vector<int> >::iterator it;
vector<int>::const_iterator any, is_in;

while (!my_map.empty()){
         it = my_map.begin();
         list.push_back(it->first);
         list.insert(list.end(), (it->second).begin(), (it->second).end());
         my_map.erase(it); // erase by iterator
         //go to next key and take its elements, if one is not inside add into list
         int newsize = list.size();
         for (int next=1; next<newsize; next++){          
              vector<int>& neb_to_next_element = my_map[list[next]];                
              for (any=neb_to_next_element.begin();
                     any!=neb_to_next_element.end(); any++){            
                   is_in = find (list.begin(), list.end(), *any);           
                   if(is_in==list.end()) list.push_back(*any);
              }
              //remove next now
              my_map.erase(list[next]);
              newsize = list.size();      
         }
         listoflist.push_back(list);
         list.clear();
}

here is part of my map

5 7 9
7 5 9 11
9 5 7 11
11 7 9
14 15 16 17
15 14 17
16 14 17 21
17 14 15 16 21
21 16 17
25 26
26 25

i want a vector of vector something like as follows

5 7 9 11
14 15 16 17 21
25 26

expecting your suggestions.

  • 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-27T05:47:39+00:00Added an answer on May 27, 2026 at 5:47 am

    How about creating a vector of sets for the output:

    typedef std::set<int>                   one_collection;
    typedef std::vector<one_collection>     all_collections;
    typedef std::map<int, std::vector<int>> source_data;
    
    source_data     src;
    all_collections dst;
    
    while (!src.empty())
    {
        dst.push_back(one_collection());
        one_collection & c = dst.back();
    
        source_data::const_iterator const it = src.begin();
    
        c.insert(it->first);
        c.insert(it->second.begin(), it->second.end());
    
        src.erase(it);
    
        for (one_collection::const_iterator jt = c.begin(), end = c.end(); jt != end; ++jt)
        {
            source_data::const_iterator const kt = src.find(*jt);
    
            if (kt == src.end()) continue;
    
            c.insert(kt->second.begin(), kt->second.end());
            src.erase(kt);
        }
    }
    

    Now each set in dst should contain one “connected collection”.

    (Pre-C++11 your iterators may have to be mutable-iterators, since erase() was broken previously.)

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

Sidebar

Related Questions

Have a Map which contains objects that I want to keep in sync across
So I have a container(any kind, probably std::map or std::vector) which contains objects of
I have a map class, which contains a vector containing MapEntitys. MapEntity is a
I have a class which contains a static member, a map of strings to
I have an entity object which contains two element collections as such: @Entity public
I have an ASP.NET web site which contains some ImageButton controls that cause postbacks
I have a contacts table which contains fields such as postcode , first name
I have an XML file which contains multiple nodes that have children. If the
I have a service running in my Android application which contains a HashMap that
I have a basic map view in my app which contains a set of

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.