我有个问题.当我使用带有自定义比较器的std :: set时,其他操作(如erase或count)无法正常工作.例如:
int sz(int const & n) {
return __builtin_popcount(n);
}
struct comp {
bool operator()(int const & a, int const & b) const {
return sz(a) >= sz(b);
}
};
void solve() {
set<int, comp> s;
for (int i = 0; i < 10; ++i)
s.insert(i);
for (int x : s)
cerr << x << " ";
cerr << "\n";
for (int i = 0; i < 10; ++i)
cerr << s.count(i) << " ";
}
Run Code Online (Sandbox Code Playgroud)
输出将是:
7 …Run Code Online (Sandbox Code Playgroud)