C++ STL概述
一、STL总体概述
中文 | English |
STL(标准模板库)是C++标准库的重要组成部分,提供通用、高效的数据结构和算法实现,支持泛型编程。 | STL (Standard Template Library) is a key part of the C++ standard library, providing generic, efficient data structures and algorithms supporting generic programming. |
设计目标: | Design goals: |
– 提高开发效率,避免重复造轮子 | – Improve development efficiency and avoid reinventing the wheel |
– 实现代码复用,支持类型无关编程 | – Enable code reuse and type-independent programming |
– 保证良好的性能和跨平台一致性 | – Ensure good performance and cross-platform consistency |
STL包含五大模块: | STL consists of five main components: |
1. 容器(Containers) | 1. Containers |
2. 迭代器(Iterators) | 2. Iterators |
3. 算法(Algorithms) | 3. Algorithms |
4. 函数对象(Functors) | 4. Functors |
5. 适配器(Adapters) | 5. Adapters |
二、容器(Containers)
中文 | English |
容器用于存储和管理对象。STL容器分为两类: | Containers store and manage objects. STL containers are divided into two categories: |
1. 序列式容器(Sequence Containers) | 1. Sequence Containers |
2. 关联式容器(Associative Containers) | 2. Associative Containers |
1. 序列式容器(Sequence Containers)
中文 | English |
vector — 动态数组,支持快速随机访问,尾部插入效率高。 | vector — Dynamic array supporting fast random access and efficient insertion at the end. |
std::vector<int> v = {1, 2, 3};
v.push_back(4);
for (auto n : v)
std::cout << n << ” “; // 输出:1 2 3 4
中文 | English |
list — 双向链表,支持高效插入和删除,随机访问效率低。 | list — Doubly linked list, efficient insertion and deletion, but slow random access. |
std::list<int> l = {1, 2, 3};
l.push_front(0);
for (auto n : l)
std::cout << n << ” “; // 输出:0 1 2 3
中文 | English |
deque — 双端队列,支持两端高效插入和删除,随机访问效率接近vector。 | deque — Double-ended queue, supports efficient insertion and deletion at both ends, near vector random access speed. |
std::deque<int> d = {1, 2, 3};
d.push_back(4);
d.push_front(0);
for (auto n : d)
std::cout << n << ” “; // 输出:0 1 2 3 4
2. 关联式容器(Associative Containers)
中文 | English |
set — 有序集合,元素唯一,基于红黑树实现。 | set — Ordered collection of unique elements, implemented as a red-black tree. |
std::set<int> s = {3, 1, 4};
s.insert(2);
for (auto n : s)
std::cout << n << ” “; // 输出:1 2 3 4
中文 | English |
map — 有序键值对集合,键唯一。 | map — Ordered collection of key-value pairs with unique keys. |
std::map<std::string, int> ages;
ages[“Alice”] = 30;
ages[“Bob”] = 25;
std::cout << ages[“Alice”] << std::endl; // 输出:30
中文 | English |
unordered_set / unordered_map — 无序版本,基于哈希表实现,平均访问效率更高。 | unordered_set / unordered_map — Unordered versions based on hash tables, providing faster average access. |
三、迭代器(Iterators)
中文 | English |
迭代器类似指针,是遍历容器元素的通用接口。 | Iterators act like pointers and provide a generic way to traverse container elements. |
迭代器类别包括输入迭代器、输出迭代器、前向迭代器、双向迭代器、随机访问迭代器。 | Categories include input, output, forward, bidirectional, and random-access iterators. |
std::vector<int> v = {10, 20, 30};
for (auto it = v.begin(); it != v.end(); ++it) {
std::cout << *it << ” “; // 输出:10 20 30
}
四、算法(Algorithms)
中文 | English |
STL算法库提供丰富的通用算法,通常通过迭代器操作容器。 | STL provides a rich set of generic algorithms, usually operating via iterators. |
中文 | English |
排序 | Sorting |
std::vector<int> v = {3, 1, 4};
std::sort(v.begin(), v.end()); // 排序
中文 | English |
查找 | Searching |
if (std::binary_search(v.begin(), v.end(), 4)) {
std::cout << “Found 4\n”;
}
中文 | English |
复制 | Copying |
std::vector<int> v2(3);
std::copy(v.begin(), v.end(), v2.begin());
中文 | English |
变换 | Transforming |
std::transform(v.begin(), v.end(), v.begin(), [](int n) { return n * 2; });
五、函数对象(Functors)
中文 | English |
函数对象是定义了 operator() 的对象,可以像函数一样调用。常用于自定义算法行为。 | Functors are objects that define operator(), allowing them to be called like functions. Often used to customize algorithm behavior. |
struct Descending {
bool operator()(int a, int b) const {
return a > b;
}
};
std::sort(v.begin(), v.end(), Descending());
六、适配器(Adapters)
中文 | English |
适配器封装现有容器或迭代器,提供不同接口。 | Adapters wrap existing containers or iterators to provide different interfaces. |
容器适配器包括: | Container adapters include: |
– stack — 后进先出(LIFO) | – stack — Last-In-First-Out (LIFO) |
– queue — 先进先出(FIFO) | – queue — First-In-First-Out (FIFO) |
– priority_queue — 优先队列 | – priority_queue — Priority queue |
std::stack<int> s;
s.push(10);
s.push(20);
std::cout << s.top() << std::endl; // 输出:20
s.pop();
std::cout << s.top() << std::endl; // 输出:10