读书人

linux上练习 c++ 库函数排序使用举例

发布时间: 2012-10-23 12:12:22 作者: rapoo

linux下练习 c++ 库函数排序使用举例

//使用库函数排序举例#include <iostream>#include <string>#include <algorithm>//内有排序库函数using namespace std;#ifndef person_h_1  //预定义指令#define person_h_1class person{public:person(const char* name,int age):name(name),age(age){}//构造函数friend ostream& operator<<(ostream& o,const person& p)//重载输出{return o<<p.name<<":"<<p.age<<' ';}friend bool operator <(const person& a,const person& b)//重载小于{return a.age<b.age;}private:string name;int age;};#endiftemplate<typename T>void print(T b,T e)//输出数组内容{bool isnull=true;while (b!=e){cout<<*b++<<' ';isnull=false;}if(isnull==false) cout<<endl;}int main(){int a[6]={5,8,6,4,9,1};double b[4]={4.4,3.2,6.7,1.2};string c[5]={"yeah","are","you","people","good"};person p[3]={person("ppp",23),person("kkk",21),person("mmm",20)};sort(a,a+6);//排序sort(b,b+4);sort(c,c+5);sort(p,p+3);print(a,a+6);//输出print(b,b+4);print(c,c+5);print(p,p+3);}

读书人网 >C++

热点推荐