Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert std::vector to std::set without losing the order

I am using the below code for conversion:

std::set<ObjectType> s(v.begin(), v.end());

However, I need to keep the order of the vector elements. How can I do this?

like image 810
eaytan Avatar asked Mar 18 '26 01:03

eaytan


1 Answers

You can't if the vector is not ordered. A std::set keeps it contents in either an ascending or descending order.

If the vector is ordered then you just need to set the comparison function of the std::set to whatever was used to order the vector.

You may want to see: How to remove duplicates from unsorted std::vector while keeping the original ordering using algorithms?

like image 129
NathanOliver Avatar answered Mar 20 '26 18:03

NathanOliver



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!