【C++】STL常用算法

本文详细介绍了C++ STL中的遍历、查找、排序、拷贝替换、算术生成和集合操作等常见算法,包括for_each、transform、find、find_if、sort、random_shuffle、copy、replace、accumulate等,帮助开发者更好地理解和使用STL。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、遍历算法

1. for_each(iterator beg, iterator end, _func);

遍历容器。

2. transform(iterator beg1, iterator end1, iterator beg2, _func)

搬运容器,目标容器需要提前开辟空间。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Print
{
   
public:
	void operator()(int val) {
   
		cout << val << " ";
	}
};

void test01() {
   
	vector<int> vec = {
    0,1,2,3,4,5,6,7,8,9 };
	for_each(vec.begin(), vec.end(), Print());
	cout << endl;
}

class Transform
{
   
public:
	int operator()(int val) {
   
		return val;
	}
};

void test02()
{
   
	vector<int> vec = {
    0,1,2,3,4,5,6,7,8,9 };
	// 目标容器
	vector<int> vTarget;
	// 目标容器需要提前开辟空间
	vTarget.resize(vec.size());

	transform(vec.begin(), vec.end(), vTarget.begin(), Transform());
	for_each(vTarget.begin(), vTarget.end(), Print());
}

int main()
{
   
	test01();
	test02();
	system("pause");
	return 0;
}

二、查找算法

1. find(iterator beg, iterator end, value);

按值查找元素,找到返回指定位置的迭代器,找不到返回尾后迭代器。 查找自定义数据类型时,确保该类型定义了==运算符。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

// 查找内置数据类型
void test01()
{
   
	vector<int> vec = {
    0,1,2,3,4,5,6,7,8,9 };
	// 查找容器中是否有5这个元素
	vector<int>::iterator it = find(vec.begin(), vec.end(), 5);
	if (it == vec.end()) {
   
		cout << "没有找到!" << endl;
	}
	else {
   
		cout << "找到!" << endl;
	}
}

// 查找自定义数据类型
class Person {
   
public:
	Person(string name, int age) {
   
		this->m_Name = name;
		this->m_Age = age;
	}
	// ==运算符
	bool operator==(const Person& p) {
   
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
   
			return true;
		}
		return false;
	}
public:
	string m_Name;
	int m_Age;
};

void test02()
{
   
	vector<Person> vec;
	vec.emplace_back("aaa", 10);
	vec.emplace_back("bbb", 20);
	vec.emplace_back("ccc", 30);
	vec.emplace_back("ddd", 40);

	vector<Person>::iterator it = find(vec.begin(), vec.end(), Person("aaa", 10));
	if (it == vec.end()) {
   
		cout << "没有找到!" << endl;
	}
	else {
   
		cout << "找到!" << endl;
	}
}

int main()
{
   
	test01();
	test02();
	system("pause");
	return 0;
}

2. find_if(iterator beg, iterator end, _Pred);

按值查找元素,找到返回指定位置的迭代器,找不到返回尾后迭代器。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

// 查找内置数据类型
class GreaterFive
{
   
public:
	bool operator()(int val) {
   
		return val > 5;
	}
};

void test01()
{
   
	vector<int> vec = {
    0,1,2,3,4,5,6,7,8,9 };
	vector<int>::iterator it = find_if(vec.begin(), vec.end(), GreaterFive());
	if (it == vec.end()) {
   
		cout << "没有找到!" << endl;
	}
	else {
   
		cout << "找到!" << endl;
	}
}

// 查找自定义数据类型
class Person {
   
public:
	Person(string name, int age) {
   
		this->m_Name = name;
		this->m_Age = age;
	}
public:
	string m_Name;
	int m_Age;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值