Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove elements from first set element which second set contains without iteration

Tags:

c++

stl

I have two sets of pairs ( I cannot use c++11)

std::set<std::pair<int,int> > first;
std::set<std::pair<int,int> > second;

and I need to remove from first set all elements which are in second set(if first contain element from second to remove). I can do this by iterating through second set and if first contains same element erase from first element, but I wonder is there way to do this without iteration ?

like image 446
Damir Avatar asked Jan 25 '26 22:01

Damir


1 Answers

If I understand correctly, basically you want to calculate the difference of first and second. There is an <algorithm> function for that.

std::set<std::pair<int, int>> result;
std::set_difference(first.begin(), first.end(), second.begin(), second.end(), inserter(result, result.end()));
like image 154
Armen Tsirunyan Avatar answered Jan 27 '26 10:01

Armen Tsirunyan