struct内部排序
#include<vector>
#include<algorIThm>
using namespace std;
typedef struct ValuePerWeight //定义结构体
{
double vw;
int i;
bool operator <(const ValuePerWeight &other)const //升序排序
{
return vw>other.vw;
}
//或者
bool operator >(const ValuePerWeight &r)const //降序排序
{
return vw>other.vw;
}
}ValuePerWeight;
int main(){
ValuePerWeight a1..an;
vector<ValuePerWeight> vpw ;
for(int i=1;i<=n;i++){
....
vpw.push_back(ai);//对a1到an处理后加到vpw
}
sort(vpw.begin(), vpw.end(),greater<ValuePerWeight>());//降序
sort(vpw.begin(), vpw.end(),less<ValuePerWeight>());//升序
//默认情况为升序 sort(vpw.begin(), vpw.end())
....
return 0;
}
结构体外排序
#include<vector>
#include<algorIThm>
#include <functional>
using