STL:遍历、查找、排序、拷贝和替换、算术生成、集合算法
前言
本文包含STL常用遍历算法(for_each、transform)、STL常用查找算法(find、find_if、adjacent_find、binary_search、count、count_if)、STL常用排序算法(sort、random_shuffle、merge、reverse)、STL常用拷贝和替换算法(copy、replace、replace_if、swap)、STL常用算术生成算法(accumulate、fill)、STL常用集合算法(set_intersection、set_union、set_difference)。
概述:
(1)、算法主要是由头文件 <algorithm>
<functional>
<numeric>
组成
(2)、<algorithm>
是所有 STL 头文件中最大的一个,范围涉及到比较、 交换、查找、遍历操作、复制、修改等等
(3)、<numeric>
体积很小,只包括几个在序列上面进行简单数学运算的模板函数
(4)、<functional>
定义了一些模板类,用以声明函数对象 内建函数对象
1 常用遍历算法
学习目标: 掌握常用的遍历算法
算法简介:
(1)、 for_each
遍历容器
(2)、 transform
搬运容器到另一个容器中
1.1 for_each
功能描述: 实现遍历容器
函数原型:
-
for_each(iterator beg, iterator end, _func);
// 遍历算法 遍历容器元素
// beg 开始迭代器
// end 结束迭代器
// _func 函数或者函数对象 (函数或者仿函数)
// 常用遍历算法 for_each
#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间
#include <algorithm>
#include <vector>
// 普通函数
void print01(int val)
{
cout << val << " "; // 0 1 2 3 4 5 6 7 8 9
}
// 函数对象;仿函数
class print02
{
public:
void operator()(int val)
{
cout << val << " "; // 0 1 2 3 4 5 6 7 8 9
}
};
// for_each算法基本用法
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
// 遍历算法
for_each(v.begin(), v.end(), print01); // print01:普通函数放入函数名即可
cout << endl;
for_each(v.begin(), v.end(), print02()); // print02():仿函数需传入函数对象
cout << endl;
}
int main() {
test01();
cout << endl;
system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果
return 0; // 程序正常退出
}
总结: for_each 在实际开发中是最常用遍历算法,需要熟练掌握
1.2 transform
功能描述: 搬运容器到另一个容器中
函数原型:
transform(iterator beg1, iterator end1, iterator beg2, _func);
// beg1 源容器开始迭代器
// end1 源容器结束迭代器
// beg2 目标容器开始迭代器
// _func 函数或者函数对象 ==(回调函数、仿函数)在搬运过程中,期间可以对数据进行逻辑运算:加减乘除等
// 常用遍历算法 搬运 transform
#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间
#include <algorithm>
#include <vector>
class TransForm
{
public:
int operator()(int val)
{
return val; // return val + 100;
}
};
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>vTarget; // 目标容器
vTarget.resize(v.size()); // 目标容器需要提前开辟空间
transform(v.begin(), v.end(), vTarget.begin(), TransForm());
for_each(vTarget.begin(), vTarget.end(), MyPrint());
}
int main() {
test01();
cout << endl;
system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果
return 0; // 程序正常退出
}
总结: 搬运的目标容器必须要提前开辟空间,否则无法正常搬运
2 常用查找算法
学习目标: 掌握常用的查找算法
算法简介:
(1)、 find
查找元素
(2)、 find_if
按条件查找元素
(3)、 adjacent_find
查找相邻重复元素
(4)、 binary_search
二分查找法
(5)、 count
统计元素个数
(6)、 count_if
按条件统计元素个数
2.1 find
功能描述: 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器 end()
函数原型:
-
find(iterator beg, iterator end, value);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素
// 常用查找算法:find
#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间
#include <algorithm>
#include <vector>
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i + 1);
}
// 查找容器中是否有 5 这个元素
vector<int>::iterator it = find(v.begin(), v.end(), 5);
if (it == v.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到:" << *it << endl;
}
}
class Person {
public:
Person(string name, int age) // 构造函数,初始化
{
this->m_Name = name;
this->m_Age = age;
}
// 重载==;让底层find知道如何对比person数据类型
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> v;
// 创建数据
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
Person pp("bbb", 20);
vector<Person>::iterator it = find(v.begin(), v.end(), p2);
if (it == v.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到姓名:" << it->m_Name << " 年龄: " << it->m_Age << endl;
}
}
int main() {
test01();
cout << endl;
test02();
cout << endl;
system("pause"); // 相当于在本地 Windows 调试器中的:请按任意键继续...;暂停,方便看清楚输出结果
return 0; // 程序正常退出
}
总结: 利用find可以在容器中找指定的元素,返回值是 迭代器
2.2 find_if
功能描述: 按条件查找元素
函数原型:
-
find_if(iterator beg, iterator end, _Pred);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// _Pred 函数或者谓词(返回 bool 类型的仿函数)
// 常用查找算法,find_if
#include <iostream> // 包含标准输入输出流头文件
using namespace std; // 使用标准命名空间
#include <algorithm>
#include <vector>
// 1、查找内置数据类型
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i + 1);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
if (it == v.end()) {
cout << "没有找到!" << endl;
}
else {
cout << "找到大于5的数字:" << *it << endl; // 找到大于5的数字:
}
}
// 2、查找自定义数据类型
class Person {
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
public:
string m_Name;
int m_Age;
};
class Greater20
{
public:
bool operator()(Person& p)
{
return p.m_Age > 20;
}
};
void