小编Nik*_*hin的帖子

使用自定义比较器的std :: set操作

我有个问题.当我使用带有自定义比较器的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)

c++ std stdset c++11

1
推荐指数
1
解决办法
889
查看次数

标签 统计

c++ ×1

c++11 ×1

std ×1

stdset ×1