直接上代码
#include <iostream>
#include<algorithm>
using namespace std;
struct node
{
int value;
int index;
};
bool cmp(struct node a, struct node b)
{
if(a.value<b.value){
return true;
}
return false;
}
int main()
{
int zhenjishuH[5]={11,33,22,55,44};
int file_cnt=5;
node*a=new node[file_cnt];
for (int i = 0; i < file_cnt; ++i) {
a[i].index=i;
a[i].value=zhenjishuH[i];
}
sort(a,a+file_cnt,cmp);
for(int i=0;i<file_cnt;i++)
{
cout << a[i].value << " " << a[i].index << endl;
}
delete []a;
return 0;
}
输出结果
11 0
22 2
33 1
44 4
55 3
matlab下
>> [B I]=sort([11,33,22,55,44])
B =
11 22 33 44 55
I =
1 3 2 5 4
matlab下标从1开始因此 1 3 2 5 4对应C++ 0 2 1 4 3,结果一致