真爱无限的知识驿站

学习积累技术经验,提升自身能力

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

code:

//使用库函数排序举例
#include <iostream>
#include <string>
#include <algorithm>//内有排序库函数
using namespace std;
#ifndef person_h_1  //预定义指令
#define person_h_1
class 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;
};
#endif
template<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);
}


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-BlogPHP 1.7.3

Copyright 2024-2027 pukuimin Rights Reserved.
粤ICP备17100155号