Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two std::list lists and get different objects

Is there ready function to get difference of two std::list lists?

For example I have list1: obj1, obj2, obj3, obj4 and list2: obj2, obj3, obj4, obj5.

The function result should be list1_unique: obj1, list1_and_list2: obj2, obj3, obj4, list2_unique: obj5.

(It's not hard to write my own implementation but I would prefer standard function)

like image 583
brigadir Avatar asked Aug 20 '26 19:08

brigadir


1 Answers

If your lists are sorted (as your examples appear to be), std::set_difference to get list1_unique and list2_unique, and std::set_intersection to get list1_and_list2. If they're not already sorted, you could sort them with std::list::sort().

like image 191
Fred Larson Avatar answered Aug 22 '26 09:08

Fred Larson



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!