自定义set比较函数

    xiaoxiao2026-01-01  9

    #include <iostream> #include <set> #include <string> using namespace std; struct node { int a,b; node(int aa, int bb) {a=(aa),b=(bb);} }; struct cmp { bool operator()(node s1,node s2) { if( s1.a != s2.a ) return s1.a > s2.a; return s1.b>s2.b; } }; set<node, cmp>words; int main() { words.insert(node(1,1)); words.insert(node(2,1)); words.insert(node(3,2)); words.insert(node(4,3)); words.insert(node(5,3)); words.insert(node(5,-2)); set<node>::iterator mbegin; set<node>::iterator mend = words.end(); for( mbegin = words.begin(); mbegin != mend; ++mbegin) cout<<mbegin->a<<" "<<mbegin->b<<endl; return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1305553.html
    最新回复(0)