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

简介: //使用库函数排序举例 #include #include #include //内有排序库函数 using namespace std; #ifndef person_h_1 //预定义指令 #define person_h_...
//使用库函数排序举例
#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);
}


 

相关文章
|
17小时前
|
C++ 编译器 程序员
C++ 从零基础到入门(3)—— 函数基础知识
C++ 从零基础到入门(3)—— 函数基础知识
|
5天前
|
自然语言处理 编译器 C语言
【C++】C++ 入门 — 命名空间,输入输出,函数新特性
本文章是我对C++学习的开始,很荣幸与大家一同进步。 首先我先介绍一下C++,C++是上个世纪为了解决软件危机所创立 的一项面向对象的编程语言(OOP思想)。
31 1
【C++】C++ 入门 — 命名空间,输入输出,函数新特性
|
6天前
|
JSON Java Linux
【探索Linux】P.30(序列化和反序列化 | JSON序列化库 [ C++ ] )
【探索Linux】P.30(序列化和反序列化 | JSON序列化库 [ C++ ] )
20 2
|
6天前
|
存储 安全 算法
【Linux | C++ 】基于环形队列的多生产者多消费者模型(Linux系统下C++ 代码模拟实现)
【Linux | C++ 】基于环形队列的多生产者多消费者模型(Linux系统下C++ 代码模拟实现)
22 0
|
6天前
|
存储 算法 对象存储
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
14 1
|
6天前
|
算法 Linux 数据安全/隐私保护
【Linux | C++ 】生产者消费者模型(Linux系统下C++ 代码模拟实现)
【Linux | C++ 】生产者消费者模型(Linux系统下C++ 代码模拟实现)
12 0
|
6天前
|
存储 算法 数据安全/隐私保护
【C++入门到精通】 哈希结构 | 哈希冲突 | 哈希函数 | 闭散列 | 开散列 [ C++入门 ]
【C++入门到精通】 哈希结构 | 哈希冲突 | 哈希函数 | 闭散列 | 开散列 [ C++入门 ]
7 0
|
7天前
|
存储 自然语言处理 C++
刷题用到的非常有用的函数c++(持续更新)
刷题用到的非常有用的函数c++(持续更新)
14 1
|
8天前
|
存储 编译器 C++
【C++】内存管理和模板基础(new、delete、类及函数模板)
【C++】内存管理和模板基础(new、delete、类及函数模板)
21 1
|
14天前
|
存储 C++
c/c++宏定义(函数)
c/c++宏定义(函数)