注册 登录
编程论坛 C++教室

问一个 pair 用sort排序的问题

wmxl56 发布于 2015-08-23 17:02, 1723 次点击
程序代码:
pair<int,int>st[100],sd[100];
typedef pair<int,int>P;

int cmp(P a,  P b)
{
    if(a.second!=b.second)return a.second<b.second;
    if(a.first!=b.first)return a.first>b.first;
}
主函数里
sort(st,st+n,cmp);
这么写对吗?
4 回复
#2
wmxl562015-08-23 17:03
我看到网上有人是这么写的,这两种有什么区别?
程序代码:
int cmp(const P &a, const P&b)
{
    if(a.second!=b.second)return a.second<b.second;
    if(a.first!=b.first)return a.first>b.first;
}

#3
yangfrancis2015-08-23 22:54
传递引用是为了让函数体内对实参的修改能在函数体外继续有效。这里好像用不用引用都可以吧。
#4
wmxl562015-08-24 08:28
回复 3楼 yangfrancis
就是说用这两种写法都行是吗?
#5
农民工2015-08-24 08:58
是的,但你的效率没人家的高。自己想想,为何没人家好
1