C++入门教程:第六篇 - 标准模板库(STL)基础
标准模板库(STL)是C++的一部分,它提供了一组通用的模板类和函数,帮助程序员快速编写高效、可靠的代码。STL主要包括容器、算法和迭代器三个组件,这些组件可以与模板机制结合使用,使得代码更加灵活和强大。本文将介绍STL的基本组成部分及其使用方法。
1. 容器
STL容器是用来存储数据的类模板。C++标准库提供了多种容器,每种容器都有其特定的用途和性能特点。
1.1 顺序容器
顺序容器按照插入顺序存储元素,支持快速访问和修改操作。常见的顺序容器包括vector
、list
和deque
。
1.1.1 vector
vector
是一个动态数组,它可以在运行时自动调整大小,支持随机访问和高效的末尾插入操作。
cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
// 添加元素
numbers.push_back(6);
// 访问元素
for (int i : numbers) {
cout << i << " ";
}
cout << endl;
return 0;
}
cpp
1.1.2 list
list
是一个双向链表,它支持高效的插入和删除操作,但不支持随机访问。
cpp
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> numbers = {1, 2, 3, 4, 5};
// 添加元素
numbers.push_back(6);
numbers.push_front(0);
// 访问元素
for (int i : numbers) {
cout << i << " ";
}
cout << endl;
return 0;
}
cpp
1.1.3 deque
deque
是一个双端队列,它支持在两端进行高效的插入和删除操作,适合需要频繁在两端操作的场景。
cpp
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> numbers = {1, 2, 3, 4, 5};
// 添加元素
numbers.push_back(6);
numbers.push_front(0);
// 访问元素
for (int i : numbers) {
cout << i << " ";
}
cout << endl;
return 0;
}
cpp
1.2 关联容器
关联容器用于存储键值对,并按照键的顺序进行排序。常见的关联容器包括set
、map
、multiset
和multimap
。
1.2.1 set
set
是一个不允许重复元素的容器,自动按升序排列元素。
cpp
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> numbers = {1, 2, 3, 4, 5};
// 添加元素
numbers.insert(6);
// 访问元素
for (int i : numbers) {
cout << i << " ";
}
cout << endl;
return 0;
}
cpp
1.2.2 map
map
是一个存储键值对的容器,其中每个键都是唯一的,并且按键的升序排列。
cpp
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> age = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};
// 添加元素
age["Diana"] = 28;
// 访问元素
for (const auto& pair : age) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}
cpp
1.2.3 multiset
和 multimap
multiset
和multimap
允许存储重复的元素和键值对,适用于需要处理重复数据的场景。
cpp
#include <iostream>
#include <set>
#include <map>
using namespace std;
int main() {
multiset<int> numbers = {1, 2, 2, 3, 4, 4, 4, 5};
// 添加元素
numbers.insert(6);
// 访问元素
for (int i : numbers) {
cout << i << " ";
}
cout << endl;
multimap<string, int> age = {{"Alice", 30}, {"Bob", 25}, {"Alice", 28}};
// 访问元素
for (const auto& pair : age) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}
cpp
2. 算法
STL算法是一组用于操作容器中元素的模板函数,支持排序、查找、修改等操作。
2.1 常见算法
2.1.1 排序
sort
算法可以对容器中的元素进行排序。
cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {5, 2, 9, 1, 5, 6};
// 排序
sort(numbers.begin(), numbers.end());
// 访问元素
for (int i : numbers) {
cout << i << " ";
}
cout << endl;
return 0;
}
cpp
2.1.2 查找
find
算法用于在容器中查找特定元素。
cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
// 查找元素
auto it = find(numbers.begin(), numbers.end(), 3);
if (it != numbers.end()) {
cout << "Element found: " << *it << endl;
} else {
cout << "Element not found" << endl;
}
return 0;
}
cpp
2.1.3 变换
transform
算法用于对容器中的元素进行变换操作。
cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
vector<int> squares(numbers.size());
// 变换操作:计算平方
transform(numbers.begin(), numbers.end(), squares.begin(), [](int x) { return x * x; });
// 访问变换后的元素
for (int i : squares) {
cout << i << " ";
}
cout << endl;
return 0;
}
cpp
3. 迭代器
迭代器是用于遍历容器元素的对象,类似于指针,但提供了更强大的功能。STL中的迭代器支持不同类型的容器和算法。
3.1 迭代器的基本使用
3.1.1 普通迭代器
以下示例展示了如何使用迭代器遍历vector
中的元素:
cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
// 使用迭代器遍历元素
for (auto it = numbers.begin(); it != numbers.end(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
cpp
3.1.2 反向迭代器
反向迭代器用于从容器的末尾向前遍历元素。
cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
// 使用反向迭代器遍历元素
for (auto it = numbers.rbegin(); it != numbers.rend(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
cpp
4. 总结
本文介绍了C++标准模板库(STL)的基本组成部分,包括容器、算法和迭代器。STL为C++程序员提供了强大的工具来处理数据集合,提高程序的效率和可维护性。掌握STL的使用将大大简化你的编程工作,并提升代码质量。在下一篇教程中,我们再见!