I have
vector<string> data ; // I hold some usernames in it
In that vector, I have duplicate element(s), so I want erase this/these element(s).Are there any algorithm or开发者_Python百科 library function to erase duplicate element(s)?
ex :
In data;
abba, abraham, edie, Abba, edie
After operation;
abba, abraham, edie, Abba
If you can sort the elements in the container, the straightforward and relatively efficient solution would be:
std::sort(data.begin(), data.end());
data.erase(std::unique(data.begin(), data.end()), data.end());
I'm not sure there is a really good way to do it. What I would do is sort (in a different array, if you need the original in tact) and then run through it.
"set" does not allow duplicates. You can use that to filter out duplicates.
- Create a set
- Add all usernames to set
- Create a new vector
- Add all elements from set to vector
If you really need to do it efficiently, then should do an in place sort first, and then go through the container by yourself instead of using std::unique, fetch unique items into a new vector, at then end do a swap.
I just checked the source code of std::unique, it will do a lot move when finding one duplicate, move hurts vector's performance.
精彩评论