I have two sorted sets:
set<char, greater<char> > alp1;
set<char, greater<char> > alp;
I need to find the set difference: alp-alp1:
set_difference(alp.begin(), alp.end(), alp1.begin(), alp1.end(), inserter(diff1, diff1.end()));
But, only the first element of alp1 is subtracted from alp.
itr = set_difference(alp.begin(), alp.end(), alp1.begin(), alp1.end(), diff1.begin());
for(auto it=diff.begin(); it<itr; it++)
cout<<*it;
no match for ‘operator<’ (operand types are ‘std::_Rb_tree_const_iterator<char>’ and ‘std::set<char>::const_iterator’ {aka ‘std::_Rb_tree_const_iterator<char>’})
How do I solve this problem?
You need to pass a greater<char>() comparator to set_difference, the same comparator you use for your sets (see full API):
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int main() {
set<char, greater<char>> alp = {'a', 'b', 'c', 'd', 'e'};
set<char, greater<char>> alp1 = {'a', 'b', 'c'};
set<char, greater<char>> diff;
set_difference(alp.begin(), alp.end(), alp1.begin(), alp1.end(),
inserter(diff, diff.begin()), greater<char>());
for (const char c : diff) {
cout << c;
}
return 0;
}
Output:
ed
Demo: http://cpp.sh/3be2u.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With