Note that there are some explanatory texts on larger screens.

plurals
  1. POoptimising the re-arranging of a map into a vector of vector
    text
    copied!<p>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.</p> <pre><code>//populate my map from the upper part of my program vector&lt;int&gt; list; vector&lt;vector&lt;int&gt; &gt; listoflist; map&lt;int,vector&lt;int&gt; &gt;::iterator it; vector&lt;int&gt;::const_iterator any, is_in; while (!my_map.empty()){ it = my_map.begin(); list.push_back(it-&gt;first); list.insert(list.end(), (it-&gt;second).begin(), (it-&gt;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&lt;newsize; next++){ vector&lt;int&gt;&amp; 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(); } </code></pre> <p>here is part of my map</p> <pre><code>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 </code></pre> <p>i want a vector of vector something like as follows</p> <pre><code>5 7 9 11 14 15 16 17 21 25 26 </code></pre> <p>expecting your suggestions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload