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;
}
运行结果
相关推荐
感哥3 小时前
C++ STL 常用算法
c++
saltymilk14 小时前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥14 小时前
C++ lambda 匿名函数
c++
沐怡旸20 小时前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥20 小时前
C++ 内存管理
c++
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
感哥1 天前
C++ 指针和引用
c++
感哥2 天前
C++ 多态
c++
沐怡旸2 天前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River4162 天前
Javer 学 c++(十三):引用篇
c++·后端