关于sort的cmp(STL)
  • 板块学术版
  • 楼主tangxinyan
  • 当前回复5
  • 已保存回复5
  • 发布时间2024/1/19 22:00
  • 上次更新2024/1/20 10:01:53
查看原帖
关于sort的cmp(STL)
1074664
tangxinyan楼主2024/1/19 22:00

这里有三个程序,似乎都是从小到大排序,但最后aa数组的输出却是从大到小,为什么?

code

#include<bits/stdc++.h>
using namespace std;
int n,a[105],b[105];
bool cmp1(const int &x,const int &y)
{
	return a[x]<a[y];
}
bool cmp2(const int &x,const int &y)
{
	return x<y;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		b[i]=a[i];
	}
	sort(a+1,a+n+1,cmp1);
	for(int i=1;i<=n;i++)
	{
		cout<<a[i]<<' ';
	}
	puts("");
	sort(b+1,b+n+1,cmp2);
	for(int i=1;i<=n;i++)
	{
		cout<<b[i]<<' ';
	}
	return 0;
}

in

5
4 2 3 5 1

out

5 3 2 4 1
1 2 3 4 5
1 2 3 4 5
2024/1/19 22:00
加载中...