c++STL模板库的set和multiset自定义排序函数-创新互联
                                            
                                                c++ STL模板库的set和multiset自定义排序函数
当前标题:c++STL模板库的set和multiset自定义排序函数-创新互联
标题链接:http://www.scyingshan.cn/article/dehgdg.html
                                            
                                        以set具体说明

#include#includeusing namespace std;
struct node
{int x;
    int y;
    
    bool operator<(const node& a) const
    {if (a.x == x)
            return y< a.y;
        return x< a.x;
    }
};
//在 set 里面进行重载,和优先队列一样
void run_case()
{sets;
    s.insert(node{1, 1 });
    s.insert(node{1, 1 });
    s.insert(node{1, 2 });
    s.insert(node{1, 3 });
    s.insert(node{2, 2 });
    s.insert(node{2, 3 });
    cout<< s.begin()->x<< " "<< s.begin()->y<< "\n"; // 输出 1,1
    
}
int main()
{run_case();
	return 0;
}   #include#includeusing namespace std;
struct node
{int x;
    int y;
};
class comp
{public:
    bool operator()(const node& a, const node& b) const
    {if (a.x == b.x)
            return a.y< b.y;
        return a.x< b.x;
    }
   
};
void run_case()
{sets;
    s.insert(node{1, 1 });
    s.insert(node{1, 1 });
    s.insert(node{1, 2 });
    s.insert(node{1, 3 });
    s.insert(node{2, 2 });
    s.insert(node{2, 3 });
    cout<< s.begin()->x<< " "<< s.begin()->y<< "\n";
}
int main()
{run_case();
	return 0;
}   #include#includeusing namespace std;
struct node
{int x;
    int y;
};
bool comp(const node& a, const node& b)
{if (a.x == b.x)
        return a.y< b.y;
    return a.x< b.x;
}
void run_case()
{sets(comp);
    s.insert(node{1, 1 });
    s.insert(node{1, 1 });
    s.insert(node{1, 2 });
    s.insert(node{1, 3 });
    s.insert(node{2, 2 });
    s.insert(node{2, 3 });
    cout<< s.begin()->x<< " "<< s.begin()->y<< "\n";
}
int main()
{run_case();
	return 0;
}   对于multiset也是一样的用法,可以自行尝试
参考资料点这里
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
当前标题:c++STL模板库的set和multiset自定义排序函数-创新互联
标题链接:http://www.scyingshan.cn/article/dehgdg.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 