我有两个排序集:
set<char, greater<char> > alp1;
set<char, greater<char> > alp;
我需要找到设置差异: alp-alp1 :
set_difference(alp.begin(), alp.end(), alp1.begin(), alp1.end(), inserter(diff1, diff1.end()));
但是,alp1
仅减去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>’})
我该如何解决这个问题?
答案 0 :(得分:6)
您需要将greater<char>()
比较器传递给set_difference
,该比较器用于您的集合(请参阅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;
}
输出:
ed