V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
96452768
V2EX  ›  C++

set 自定義函式問題

  •  
  •   96452768 · 194 天前 · 569 次点击
    这是一个创建于 194 天前的主题,其中的信息可能已经有所发展或是发生改变。
    請問為什麼這段程式碼會輸出: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 吧
    lsry
        1
    lsry  
       194 天前
    一個個插入倒是按預期的是 8 8 8 8 8 ,我覺得應該和迭代器那裏的構造函數有關係,你可以 debug 看看實現。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2693 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:31 · PVG 18:31 · LAX 03:31 · JFK 06:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.