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.
How about creating a vector of sets for the output:
Now each set in
dstshould contain one “connected collection”.(Pre-C++11 your iterators may have to be mutable-iterators, since
erase()was broken previously.)