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;