How to remove duplicates from std::vector <std::pair<UnicodeString, UnicodeString> >
h开发者_如何学Goow 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
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.
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.
精彩评论