3

how to remove duplicate values from

std::vector <std::pair<UnicodeString, UnicodeString> > myVect;

Is there any built in function or i need to write a custom code for this

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Jame
  • 21,150
  • 37
  • 80
  • 107

2 Answers2

6

Assuming that (a) a std::set is not what you want [that is you want to allow duplicate elements in your std::vector, only to remove them later] and (b) you don't wish to change the order of the elements in your std::vector [that is, the current order is important], which are both reasonable situations... You should be able to adapt Fred Nurk's answer to How can I remove duplicate values from a list in C++ buy substituting vector for list and modifying the less comparators accordingly.

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • But, if he uses Fred's answer with `vector`, won't he get O(N^2) behavior, since `vector::erase()` is O(N) while `list::erase()` is O(1)? Is there a solution that works well with vectors? – Robᵩ Mar 22 '11 at 15:24
  • @Rob: Good point. Since, I don't know (a) whether `std::set` is acceptable, (b) whether @jame can `sort` the vector or (c) whether speed is an issue, I'll leave my answer as-is for now. – johnsyweb Mar 22 '11 at 20:39
2

The best way to do it, if you can modify the order in your vector is the following:

   std::sort(myVect.begin(), myVect.end());
   myVect.erase(std::unique(myVect.begin(), myVect.end()), myVect.end());

Just make sure UnicodeString accepts the < operator.

However, you may want to use a different structure such as std::set or std::unordered_set to have an unique guarantee at insertion.

Edouard A.
  • 6,100
  • 26
  • 31