验证对C++结构体排序是整体进行的

【算法代码】

#include <bits/stdc++.h>
using namespace std;
 
const int maxn=105;
int n;

struct node {
	int v,id;
} p[maxn];
 
bool cmp(node x,node y) {
	if(x.v!=y.v) return x.v<y.v;
	return x.id<y.id;
}
 
int main() {
	scanf("%d%d",&n);
	for(int i=1; i<=n; i++) {
		scanf("%d",&p[i].v);
		p[i].id=i;
	}
	
	sort(p+1,p+1+n,cmp);
	
	for(int i=1; i<=n; i++) {
		cout<<p[i].id<<" ";
	}
	
	return 0;
}
 
 
/*
in:
4 4
3 8 2 6

out:
3 1 4 2
*/



 

猜你喜欢

转载自blog.csdn.net/hnjzsyjyj/article/details/121890097
今日推荐