c++ - What's the most efficient way to erase duplicates and sort a vector? -
I probably have to take a C ++ vector with lots of elements, remove duplicates, and sort it.
I currently have the code below, but it does not work.
vec.erase (std :: unique (vec.begin (), vec.end ()), vec end ()); Std :: sort (vec.begin (), vec.end ());
How can I do this correctly?
Additionally, erasing the first duplicate (like the one coded above) or what is faster to sort first? If I sort the first, what is std :: unique
executed?
Or there is another (perhaps more efficient) way to do this. ?
I agree and; A good idea can be here even if you are trapped using vectors, if you have enough duplicates, then you might be better to create a set to do dirty work.
Let's compare the three ways:
Sort by vector, exclusive
(vec. Start (), vec.end ()); Vec.erase (unique (vec.begin (), vec.end ()), vec.end ());
Convert (manual)
set to set
Convert to set up (using constructor)
set & lt; Int & gt; S (vec.begin (), vec.end ()); Vec.assign (s.begin (), S. & ());
How do these work as a number of duplicates:
Summary : When the number of duplicates is large enough, convert it to a set to actually And then dump data back into a vector
and for some reason, the fixed conversion is progressing faster than the set constructor - At least the toy used by me on random data.
Comments
Post a Comment