14.C++常用的算法_排序算法

文章目录

遍历算法

1. sort()

代码工程
c 复制代码
sort()函数默认是升序排列,如果想要降序排列需要添加一个函数或者仿函数。
c 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

void printVector(const vector<int>&v)
{
	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << " ";
	}
	cout << endl;
}

class Greator
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};

void test01()
{
	vector<int>v;

	v.push_back(50);
	v.push_back(10);
	v.push_back(30);
	v.push_back(20);
	v.push_back(40);

	cout << "排序前: ";
	printVector(v);

	sort(v.begin(), v.end(), Greator());/*使用仿函数降序排列,不添加仿函数就是默认升序排列*/

	cout << "排序后: ";
	printVector(v);

	return;
}

int main()
{
	test01();

	return 0;
}
运行结果

2. random_shuffle()

c 复制代码
random_shuffle()函数是一个打乱容器元素排列的一种算法,俗称"洗牌"算法;
从运行的三次结果可以看出,每次打乱的顺序都是不一样的;
需要注意的是如果你没有添加随机种子,那么每次运行的结果都是一样的。
代码工程
c 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;

class print
{
public:
	void operator()(int v)
	{
		cout << v << " ";
	}
};

void test01()
{
	/*根据时间生成一个随机种子*/
	srand((unsigned int)time(NULL));

	vector<int>v;

	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	v.push_back(50);

	cout << "打乱前: ";
	for_each(v.begin(), v.end(), print());

	/*将容器中的元素顺序打乱*/
	random_shuffle(v.begin(), v.end());

	cout << endl;

	cout << "打乱后: ";
	for_each(v.begin(), v.end(), print());

	return;
}

int main()
{
	test01();

	return 0;
}
运行结果
第一次运行结果
第二次运行结果
第三次运行结果

3. merge()

c 复制代码
将两个容器的元素合并到另一个容器中;
需要注意的是目标容器需要开辟空间,开辟空间的大小为两个融合容器中元素个数之和。
代码工程
c 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class print
{
public:
	void operator()(int v)
	{
		cout << v << " ";
	}
};

void test01()
{
	vector<int>v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);

	vector<int>v2;
	v2.push_back(100);
	v2.push_back(200);
	v2.push_back(300);

	vector<int>vTarge;

	vTarge.resize(v1.size() + v2.size());

	/*将v1和v2容器的元素合并到vTarge容器中*/
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarge.begin());

	cout << "融合后: ";
	for_each(vTarge.begin(), vTarge.end(), print());

	cout << endl;

	return;
}

int main()
{
	test01();

	return 0;
}
运行结果

4. reverse()

c 复制代码
将容器中的元素进行反转
代码工程
c 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class print
{
public:
	void operator()(int v)
	{
		cout << v << " ";
	}
};

void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	v.push_back(50);

	cout << "反转前: ";
	for_each(v.begin(), v.end(), print());

	cout << endl;
	/*反转容器中的元素*/
	reverse(v.begin(), v.end());

	cout << "反转后: ";
	for_each(v.begin(), v.end(), print());

	cout << endl;

	return;
}

int main()
{
	test01();

	return 0;
}
运行结果
相关推荐
进化中的刀盾兽2 小时前
从零实现 C++ Json-Rpc(十):把消息交给真实连接——MuduoConnection 与 ConnectionFactory
c++·rpc
青瓦梦滋8 小时前
WebSocket协议
linux·服务器·网络·c++·websocket·网络协议·http
CodeYp201914 小时前
第15讲:二叉搜索树
开发语言·c++·二叉搜索树
pipixia2003314 小时前
C++继承面试四连问:内存叠加、私有继承、sizeof、权限重映射
c++·面试·嵌入式·继承·面向对象·对象模型
牵猫散步的鱼儿17 小时前
C++ 内存分区
c++
choumin17 小时前
C++ 标准容器为什么不能存引用
c++·容器·引用类型
谙弆悕博士18 小时前
用 C 写了个 Windows 聊天室,踩了几个多线程和控制台的坑
c语言·开发语言·c++·windows·网络编程·聊天室
stolentime18 小时前
CF2277E Tree Guessing题解
c++·算法·贪心算法·交互·ad-hoc
syagain_zsx19 小时前
ODB 与 MySQL:C++ 里的 ORM 实践(类即表、事务铁律与查询封装)
开发语言·数据库·c++·mysql·工具库·1024程序员节
小鱼仙官19 小时前
使用Qt封装C/C++ 动态库 用于第三方调用
c语言·c++·qt