#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;struct Node{
double h;
double w;
Node(){}
Node(double hh,double ww){h=hh;w=ww;}
};ostream& operator << (ostream& out,const Node& t)
{
return out<<t.h<<\"\t\"<<t.w;
}struct less_h:binary_function<Node,Node,bool>{
bool operator () (const Node& a,const Node& b){
return a.h<b.h;
}
};struct less_w:binary_function<Node,Node,bool>{
bool operator () (const Node& a,const Node& b){
return a.w<b.w;
}
};template <class T>
void out(T* arr,size_t n)
{
for(int i=0;i<n;i++){
cout<<arr[i]<<endl;
}
cout<<endl;
}int main()
{
Node arr[3]={Node(180.5,55),Node(172,74),Node(160,65)};cout<<\"Before sort\"<<endl;
out(arr,3);cout<<\"Sort by height\"<<endl;
sort(arr,arr+3,less_h());
out(arr,3);cout<<\"Sort by weight\"<<endl;
sort(arr,arr+3,less_w());
out(arr,3);
}
程序代码: