vendredi 27 novembre 2015

Get distinct vector of vector with count

What is the best way to return only the unique element with the counts from vector of vectors?

std::vector<std::vector<string>> vec_vec{{a,a,b,c},{a,c,c}};

The results should be :

{a, b, c} // This is the vector that contains the unique items.
{3, 1, 3} //a exists three times, b only one time, and c is three times.

To solve this I use the following:

1- Copy all the items in the vector of vector to single vector, so the output will be:

vec_vec{{a,a,b,c},{a,c,c}} -> vec{a,a,b,c,a,c,c} 

2- Now I'm dealing with a single vector (not vector of vector), so it's much easier to sort, get the unique items and them (I may use the code here1 and here2)

Is converting the vector of vector to one vector is a good idea? Any better solution?

Can we find better way with less complexity comparing with the current way (c++11, c++14)?

Aucun commentaire:

Enregistrer un commentaire