C++ STL 之常用排序算法①sort②random_shuffle③merge④reverse

VS2017程序下载:https://pan.baidu.com/s/11KR9Biaz1qsJuKp_0NT4JA?pwd=4prh

目录

[1. sort排序](#1. sort排序)

[2 random_shuffle随机排序](#2 random_shuffle随机排序)

[3. merge 合并](#3. merge 合并)

[4. reverse 反转](#4. reverse 反转)


1. sort排序

功能描述:

对容器内元素进行排序,默认升序

函数原型:

cpp 复制代码
// beg 开始迭代器 end 结束迭代器 _Pred 谓词
sort(iterator beg, iterator end, _Pred); 

程序:

cpp 复制代码
#include<iostream>
using namespace std;
#include <algorithm>
#include <vector>
#include <functional>

void myPrint(int val)
{
	cout << val << " ";
}
void test01()
{
	vector<int>v;
	
	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);
	v.push_back(40);

	cout <<  " sort默认排序: ";
	//利用sort进行排序,默认升序
	sort(v.begin(), v.end());//容器元素进行升序排列
	for_each(v.begin(), v.end(), myPrint);//遍历容器,并打印
	cout << endl;

	cout << " sort调用greater降序: ";
	//改变为 降序,template<class T> bool greater<T>    //大于,关系仿函数
	sort(v.begin(), v.end(), greater<int>());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

	cout << " sort调用less升序: ";
	//改变为 升序,template<class T> bool less<T>    //小于,关系仿函数
	sort(v.begin(), v.end(), less<int>());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;
}

int main() {

	test01();
	system("pause");
	return 0;
}

运行结果:

2 random_shuffle随机排序

功能描述:

指定范围内的元素,随机调整次序(相当于随机洗牌)

函数原型:

cpp 复制代码
// 指定范围内的元素,随机调整次序
// beg 开始迭代器 end 结束迭代器
random_shuffle(iterator beg, iterator end);

程序:

cpp 复制代码
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <ctime>

void myPrint(int val)//遍历打印数据
{
	cout << val << " ";
}

void test01()
{
	//随机时间种子,保证每次运行结果不一样
	srand((unsigned int)time(NULL));

	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);//尾插数据 0~9
	}
	cout << "打乱前数据: ";
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

	//利用洗牌 算法 打乱顺序
	random_shuffle(v.begin(), v.end());
	cout << "打乱后数据: ";
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;
}

int main() {

	test01();
	system("pause");
	return 0;
}

运行结果:

3. merge 合并

功能描述:

两个有序的容器元素合并,并存储到另一容器中

函数原型:

cpp 复制代码
// 容器元素合并,并存储到另一容器中
// 注意: 两个容器必须是有序的

// beg1 容器1开始迭代器 end1 容器1结束迭代器 beg2 容器2开始迭代器 end2 容器2结束迭代器 
// dest 目标容器开始迭代器
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

程序:

cpp 复制代码
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>

/*重载函数调用操作符的类,其对象常称为函数对象.函数对象使用重载的()时,
行为类似函数调用,也叫仿函数.函数对象(仿函数)是一个类,不是一个函数 */
//仿函数
class print02
{
public:
	void operator()(int val)//重载()
	{
		cout << val << " ";
	}
};

//普通函数
void myPrint(int val)//遍历打印数据
{
	cout << val << " ";
}

void test01()
{
	vector<int>v1;
	vector<int>v2;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i+10);
	}
	cout << " v1 原始数据: ";
	for_each(v1.begin(), v1.end(), print02());
	cout << endl;
	cout << " v2 原始数据: ";
	for_each(v2.begin(), v2.end(), print02());
	cout << endl;
	
	//目标容器
	vector<int>v3;

	//提前给目标容器分配空间
	v3.resize(v1.size() + v2.size());
	cout << " 合并v1,v2 数据,搬运给 v3 : ";
	//将容器 v1,v2的数据搬运到 vTarget,注意:容器必须有序
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	//遍历容器,并打印
	for_each(v3.begin(), v3.end(), myPrint);
	cout << endl;
}

int main() {

	test01();
	system("pause");
	return 0;
}

运行结果:

4. reverse 反转

功能描述:

将容器内元素进行反转

函数原型:

cpp 复制代码
// 反转指定范围的元素
// beg 开始迭代器 end 结束迭代器
reverse(iterator beg, iterator end); `

程序:

cpp 复制代码
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>

/*重载函数调用操作符的类,其对象常称为函数对象.函数对象使用重载的()时,
行为类似函数调用,也叫仿函数.函数对象(仿函数)是一个类,不是一个函数 */
//仿函数
class print02
{
public:
	void operator()(int val)//重载()
	{
		cout << val << " ";
	}
};

//普通函数
void myPrint(int val)//遍历打印数据
{
	cout << val << " ";
}
void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);
	v.push_back(40);

	cout << "反转前数据: " << endl;
	//遍历容器,并打印
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

	cout << "反转后数据: " << endl;
	reverse(v.begin(), v.end());//反转数据
	for_each(v.begin(), v.end(), print02());//遍历容器,并打印
	cout << endl;
}

int main() {

	test01();
	system("pause");
	return 0;
}

运行结果:

相关推荐
weixin_5375904510 分钟前
【任务6.13】计算肇事汽车号码
c++·算法·汽车
从今天开始学习Verilog19 分钟前
FFT算法实现之fft IP核
算法·fpga开发
两颗泡腾片32 分钟前
黑马程序员C++核心编程笔记--类和对象--运算符重载
c++·笔记
用户6869161349036 分钟前
1999年NOIP普及组旅行家的预算(洛谷P1016):贪心算法实战指南
c++
程序员编程指南1 小时前
Qt 与 WebService 交互开发
c语言·开发语言·c++·qt·交互
荼蘼1 小时前
基于 KNN 算法的手写数字识别项目实践
人工智能·算法·机器学习
赵英英俊1 小时前
Python day26
开发语言·python
你怎么知道我是队长1 小时前
python---eval函数
开发语言·javascript·python
溟洵1 小时前
Qt 窗口 工具栏QToolBar、状态栏StatusBar
开发语言·前端·数据库·c++·后端·qt
Yuroo zhou2 小时前
IMU的精度对无人机姿态控制意味着什么?
单片机·嵌入式硬件·算法·无人机·嵌入式实时数据库