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

运行结果:

相关推荐
墨染点香17 分钟前
LeetCode 刷题【144. 二叉树的前序遍历】
数据结构·算法·leetcode
自由随风飘4 小时前
python 题目练习1~5
开发语言·python
cynicme5 小时前
力扣3318——计算子数组的 x-sum I(偷懒版)
java·算法·leetcode
Bony-5 小时前
Go语言完全学习指南 - 从基础到精通------语言基础篇
服务器·开发语言·golang
ShineSpark6 小时前
Crashpad 在windows下编译和使用指南
c++·windows
fl1768316 小时前
基于python的天气预报系统设计和可视化数据分析源码+报告
开发语言·python·数据分析
ACP广源盛139246256737 小时前
(ACP广源盛)GSV6172---MIPI/LVDS 信号转换为 Type-C/DisplayPort 1.4/HDMI 2.0 并集成嵌入式 MCU
c语言·开发语言·单片机·嵌入式硬件·音视频
不穿格子的程序员7 小时前
从零开始刷算法-栈-括号匹配
java·开发语言·
雪域迷影7 小时前
C#中通过get请求获取api.open-meteo.com网站的天气数据
开发语言·http·c#·get
yue0087 小时前
C#类继承
java·开发语言·c#