首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-10-23 
linux下练习 c++ 库函数排序使用举例//使用库函数排序举例#include iostream#include string#include

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);}

热点排行