好的!STL(Standard Template Library,标准模板库)是C++中非常重要的一部分,它提供了许多常用的数据结构和算法。以下是STL的核心组件及其用法的复习内容:
1. STL的核心组件
STL主要由以下四个部分组成:
-
容器(Containers):用于存储数据的模板类。
-
算法(Algorithms):用于操作容器中数据的函数模板。
-
迭代器(Iterators):用于遍历容器中的元素。
-
函数对象(Functors):行为类似函数的对象,通常用于算法中。
2. 常用容器
STL提供了多种容器,以下是常见的几类:
(1) 序列容器
vector
:动态数组,支持快速随机访问。
#include <vector>
vector<int> v = {1, 2, 3};
v.push_back(4); // 添加元素
cout << v[0]; // 访问元素
list
:双向链表,支持快速插入和删除。
#include <list>
list<int> l = {1, 2, 3};
l.push_back(4); // 在末尾添加元素
l.push_front(0); // 在开头添加元素
deque
:双端队列,支持在两端快速插入和删除。
#include <deque>
deque<int> d = {1, 2, 3};
d.push_back(4); // 在末尾添加元素
d.push_front(0); // 在开头添加元素
(2) 关联容器
-
map
:键值对集合,键唯一。 -
#include <map> map<string, int> m = {{"Alice", 25}, {"Bob", 30}}; m["Charlie"] = 35; // 插入或修改元素 cout << m["Alice"]; // 访问元素
set
:有序集合,元素唯一。 -
#include <set> set<int> s = {3, 1, 2}; s.insert(4); // 插入元素 if (s.find(2) != s.end()) { cout << "2 found in set"; }
-
multiset
:允许重复元素的有序集合。 -
multimap
:允许重复键的键值对集合。
(3) 无序关联容器
-
unordered_set
:基于哈希表的集合,元素唯一。#include <unordered_set> unordered_set<int> us = {1, 2, 3}; us.insert(4); // 插入元素
unordered_map
:基于哈希表的键值对集合,键唯一。#include <unordered_map> unordered_map<string, int> um = {{"Alice", 25}, {"Bob", 30}}; um["Charlie"] = 35; // 插入或修改元素
(4) 容器适配器
-
stack
:后进先出(LIFO)的栈。#include <stack> stack<int> s; s.push(1); // 压栈 s.pop(); // 弹栈
queue
:先进先出(FIFO)的队列。#include <queue> queue<int> q; q.push(1); // 入队 q.pop(); // 出队
priority_queue
:优先队列,默认最大堆。 -
#include <queue> priority_queue<int> pq; pq.push(3); // 插入元素 pq.push(1); pq.push(2); cout << pq.top(); // 访问最大元素
3. 常用算法
STL提供了大量的算法,以下是一些常见的算法:
(1) 排序与查找
-
sort
:对容器进行排序。#include <algorithm> vector<int> v = {3, 1, 4, 1, 5}; sort(v.begin(), v.end()); // 升序排序
binary_search
:在有序容器中查找元素。 -
if (binary_search(v.begin(), v.end(), 3)) { cout << "3 found"; }
(2) 遍历与操作
-
for_each
:对容器中的每个元素执行操作。for_each(v.begin(), v.end(), [](int x) { cout << x << " "; });
transform
:对容器中的元素进行转换。 -
vector<int> v2(v.size()); transform(v.begin(), v.end(), v2.begin(), [](int x) { return x * 2; });
(3) 其他常用算法
-
find
:查找元素。auto it = find(v.begin(), v.end(), 3); if (it != v.end()) { cout << "3 found at position " << it - v.begin(); }
count
:统计元素出现的次数。int cnt = count(v.begin(), v.end(), 1);
reverse
:反转容器中的元素。 -
reverse(v.begin(), v.end());
4. 迭代器
迭代器用于遍历容器中的元素,常见的迭代器类型包括:
-
begin()
和end()
:返回指向容器开头和结尾的迭代器。 -
advance()
和next()
:移动迭代器。 -
rbegin()
和rend()
:返回反向迭代器。 -
示例:
-
vector<int> v = {1, 2, 3}; for (auto it = v.begin(); it != v.end(); it++) { cout << *it << " "; }
5. 函数对象(Functors)
函数对象是重载了
()
运算符的类对象,可以像函数一样调用。常用于算法中:struct Square { int operator()(int x) const { return x * x; } }; vector<int> v = {1, 2, 3}; transform(v.begin(), v.end(), v.begin(), Square());
6. 示例代码:综合使用STL
#include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; int main() { // 使用vector vector<int> v = {3, 1, 4, 1, 5}; sort(v.begin(), v.end()); // 排序 for (int x : v) { cout << x << " "; } cout << endl; // 使用map map<string, int> m = {{"Alice", 25}, {"Bob", 30}}; m["Charlie"] = 35; for (auto& p : m) { cout << p.first << ": " << p.second << endl; } // 使用算法 auto it = find(v.begin(), v.end(), 3); if (it != v.end()) { cout << "3 found at position " << it - v.begin() << endl; } return 0; }
总结
STL是C++中非常强大的工具,熟练掌握它可以极大地提高编程效率。建议多练习使用STL的容器和算法,并结合实际项目加深理解。
-
记得关注哦