Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove duplicates from std::vector <std::pair<UnicodeString, UnicodeString> >

Tags:

c++

stl

stdvector

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

like image 490
Jame Avatar asked Nov 24 '25 12:11

Jame


2 Answers

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.

like image 125
Johnsyweb Avatar answered Nov 26 '25 03:11

Johnsyweb


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.

like image 39
Edouard A. Avatar answered Nov 26 '25 03:11

Edouard A.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!