开发者

How to erase duplicate element in vector, efficiently

开发者 https://www.devze.com 2023-02-17 21:11 出处:网络
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百科 lib

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.

  1. Create a set
  2. Add all usernames to set
  3. Create a new vector
  4. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消