C++标准模板库(STL)的算法库是一组通用的函数模板,覆盖了排序、查找、遍历、修改等常见操作,开发者不需要自己重复实现基础算法逻辑,直接调用即可完成对应功能。STL算法库的函数大多定义在algorithm头文件中,使用时需要先包含该头文件。

STL算法库的基础使用规则
STL算法库的函数通常通过迭代器操作容器元素,大部分算法接受一对迭代器作为操作范围,左闭右开区间[first, last)内的元素会被处理。调用算法函数时需要保证迭代器类型符合函数的要求,比如sort函数需要随机访问迭代器,因此只能用于vector、array等支持随机访问的容器,不能用于list、forward_list等容器。
sort排序函数使用教程
sort函数用于对指定区间内的元素进行升序排序,默认使用小于运算符比较元素,也可以自定义比较规则。函数原型如下:
// 默认升序排序,使用operator<比较元素 template <class RandomAccessIterator> void sort (RandomAccessIterator first, RandomAccessIterator last); // 自定义比较规则排序 template <class RandomAccessIterator, class Compare> void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
基础升序排序示例
对vector容器内的整数进行升序排序,代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6};
// 对nums所有元素升序排序
std::sort(nums.begin(), nums.end());
for (int num : nums) {
std::cout << num << " ";
}
// 输出结果:1 1 2 3 4 5 6 9
return 0;
}
自定义比较规则排序
如果需要降序排序,或者自定义复杂对象的排序规则,可以传入第三个比较参数。比如对整数进行降序排序:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6};
// 降序排序,使用lambda表达式作为比较规则
std::sort(nums.begin(), nums.end(), [](int a, int b) {
return a > b;
});
for (int num : nums) {
std::cout << num << " ";
}
// 输出结果:9 6 5 4 3 2 1 1
return 0;
}
如果是自定义结构体,也可以定义比较函数或者重载小于运算符来实现排序,比如对学生结构体按成绩降序排序:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
struct Student {
std::string name;
int score;
};
int main() {
std::vector<Student> students = {
{"张三", 85},
{"李四", 92},
{"王五", 78}
};
// 按成绩降序排序
std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.score > b.score;
});
for (const auto& stu : students) {
std::cout << stu.name << " " << stu.score << std::endl;
}
// 输出结果:
// 李四 92
// 张三 85
// 王五 78
return 0;
}
find查找函数使用教程
find函数用于在指定区间内查找第一个等于目标值的元素,返回指向该元素的迭代器,如果没找到则返回区间的尾迭代器last。函数原型如下:
template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);
基础查找示例
在vector中查找指定整数,代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6};
int target = 5;
// 查找目标元素
auto it = std::find(nums.begin(), nums.end(), target);
if (it != nums.end()) {
std::cout << "找到元素" << target << ",位置是:" << (it - nums.begin()) << std::endl;
} else {
std::cout << "未找到元素" << target << std::endl;
}
// 输出结果:找到元素5,位置是:4
return 0;
}
查找自定义对象
find函数默认使用等于运算符比较元素,如果是自定义结构体,需要重载等于运算符才能正确查找,或者使用find_if函数配合自定义判断条件。比如查找指定姓名的学生:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
struct Student {
std::string name;
int score;
// 重载等于运算符,按姓名比较
bool operator==(const std::string& target_name) const {
return name == target_name;
}
};
int main() {
std::vector<Student> students = {
{"张三", 85},
{"李四", 92},
{"王五", 78}
};
std::string target_name = "李四";
// 查找姓名为李四的学生
auto it = std::find(students.begin(), students.end(), target_name);
if (it != students.end()) {
std::cout << "找到学生:" << it->name << ",成绩:" << it->score << std::endl;
} else {
std::cout << "未找到该学生" << std::endl;
}
// 输出结果:找到学生:李四,成绩:92
return 0;
}
使用STL算法的注意事项
- 调用算法前需要确认容器的迭代器类型是否满足算法要求,比如sort需要随机访问迭代器,list容器有自己的sort成员函数,不要直接用std::sort操作list。
- 算法操作的是迭代器区间,修改容器元素时如果导致迭代器失效,需要重新获取迭代器,避免未定义行为。
- 自定义比较函数或者判断条件时,要保证逻辑符合严格弱序要求,否则可能导致排序或者查找结果异常。
掌握sort和find这两个常用算法后,可以进一步学习STL算法库的其他函数,比如count统计元素个数、reverse反转区间、unique去重等,逐步提升编码效率。