set 自定義函式問題

207 天前
 96452768
請問為什麼這段程式碼會輸出:8 8

#include <iostream>
#include <vector>
#include <set>

using namespace std;

struct cmp{
bool operator()(int a,int b){
return a==b;
}

};
int main()
{
vector<int> x{8,4,6,1,4,2,7,8,8,5,8,8};
set<int,cmp> y(x.begin(),x.end());

for(auto iter=y.begin();iter!=y.end();iter++){
cout<<*iter<<" ";

}
return 0;
}

我知道 set 自定義函式不能這樣用,但我還是好奇這個輸出的原因,根據等於的定義是:!comp(a, b) && !comp(b, a),那這個輸出應該要是:8 8 8 8 8 吧
585 次点击
所在节点    C++
2 条回复
lsry
206 天前
一個個插入倒是按預期的是 8 8 8 8 8 ,我覺得應該和迭代器那裏的構造函數有關係,你可以 debug 看看實現。
araraloren
206 天前

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/982198

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX