zoukankan      html  css  js  c++  java
  • set的用法

     
    注意不能按id去重!只能id,val同时相同时才能去重!!
    struct node
    {
    int id, val;
    bool operator<(const node &x) const
    {
    if (x.id == id&&x.val==val)
    return 0;
    if (x.val == val)
    return id < x.id;
    return val < x.val;
    }
    };
    s.insert(node{10, 200});
    s.insert(node{20, 200});
    s.insert(node{30, 200});
    s.insert(node{20, 200});
    s.insert(node{10, 100});
    
    for (set<node>::iterator it = s.begin(); it != s.end(); it++)
    {
    cout << (*it).id << " " << (*it).val << endl;
    }
    struct node
    {
        int id, val;
        bool operator<(const node &x) const
        {
            if (x.id == id && x.val == val)
                return 0;
            if (x.val == val)
                return id > x.id;
            return val > x.val;
        }
    };
    int n, m, k, a, b;
    arr c;
    set<node> s;
    bool comp(const node &x,const node &y){
        return x.val > y.val;
    }
    int main()
    {
        // file("test");
        // sdf(n),sdf(m),sdf(k);
        // For(i, 1, n) sdf(c[i]);
        // sdf(a);
        // For(i,1,a)
        s.insert(node{10, 200});
        s.insert(node{20, 400});
        s.insert(node{30, 300});
        s.insert(node{20, 200});
        s.insert(node{10, 100});
        // s.clear();
        cout << s.size() << endl;
        set<node>::iterator it;
        it = upper_bound(s.begin(),s.end(),node{50, 200},comp);//从大到小排序的话,找第一个小于200的node
        if(it!=s.end())
            s.erase(it);
        cout<< s.count(node{20, 400}) << endl;
    
        for (it = s.begin(); it != s.end(); it++)
        {
            cout << (*it).id << " " << (*it).val << endl;
        }
    }
    typedef multiset<node>::iterator It;
    pair<It,It>ret=s.equal_range(x);
    cout<<*ret.first<<" "<<*ret.second;
  • 相关阅读:
    JLOI2012:时间流逝
    bzoj 5217: [Lydsy2017省队十连测]航海舰队
    bzoj 4894: 天赋
    bzoj 4870: [Shoi2017]组合数问题
    bzoj 1558: [JSOI2009]等差数列
    bzoj 4945: [Noi2017]游戏
    bzoj 2142: 礼物
    bzoj 5248: [2018多省省队联测]一双木棋
    51nod2383
    codeforces24D
  • 原文地址:https://www.cnblogs.com/planche/p/9484214.html
Copyright © 2011-2022 走看看