【发布时间】:2017-08-30 02:41:42
【问题描述】:
#include <bits/stdc++.h>
using namespace std;
int dist[2];
struct cmp {
bool operator() (const int &a, const int &b) {
return dist[a] < dist[b];
}
};
set<int, cmp> s;
int main() {
dist[0] = 2;
dist[1] = 2;
s.insert(1);
s.insert(0);
for(set<int>::iterator it = s.begin(); it != s.end(); ++it) {
cout << *it << " " << dist[*it] << endl;
}
}
以上代码输出:
1 2
为什么这是真的?如果它不输出:
1 2
0 2
谢谢!
【问题讨论】:
-
0 永远不会被添加到 s 由于比较返回 false 两次(即它认为集合中的值比较相等)。这个问题本质上是在寻求调试帮助,因此是题外话。
-
@rex:从什么时候开始询问调试帮助的问题?
-
@BenjaminLindley 我弄错了。对不起。