首先定义了一个pair<int,int> itv[10]之后给itv中的first和second赋值,最后sort(itv,itv+10)。这样子的排序是对first还是second排序?或者其他?
sort(itv, itv + 10);
先根据first排序,first相同的再根据second排序。
也可以自己定义比较函数:
// lambda,C++14sort(itv, itv + 10, [](auto a, auto b){return a.second < b.second || a.first > b.first;});
// function pointerbool comp(pair<int,int> a, pair<int,int> b) {return a.second < b.second || a.first > b.first;}sort(itv, itv + 10, comp)
// functorstruct {bool operator()(pair<int,int> a, pair<int,int> b) {return a.second < b.second || a.first > b.first;}} compf;sort(itv, itv + 10, compf)
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(1)
sort(itv, itv + 10);
先根据first排序,first相同的再根据second排序。
也可以自己定义比较函数:
// lambda,C++14
sort(itv, itv + 10, [](auto a, auto b){
return a.second < b.second || a.first > b.first;
});
// function pointer
bool comp(pair<int,int> a, pair<int,int> b) {
return a.second < b.second || a.first > b.first;
}
sort(itv, itv + 10, comp)
// functor
struct {
bool operator()(pair<int,int> a, pair<int,int> b) {
return a.second < b.second || a.first > b.first;
}
} compf;
sort(itv, itv + 10, compf)