以下是对 C++ STL(标准模板库)几个主要组件的详细解释,包括容器、迭代器、算法和函数对象,每个代码示例都有详细的注释:
一、容器(Containers)
1. vector
vector
是一个动态数组,可以在运行时调整大小。
#include <iostream>
#include <vector>
int main() {
// 创建一个空的 int 类型的 vector
std::vector<int> vec;
// 向 vector 中添加元素
vec.push_back(1); // 在 vector 的末尾添加元素 1
vec.push_back(2); // 在 vector 的末尾添加元素 2
vec.push_back(3); // 在 vector 的末尾添加元素 3
// 使用下标访问元素
std::cout << "vec[0] = " << vec[0] << std::endl; // 输出第一个元素
// 使用 at 方法访问元素,会进行边界检查
std::cout << "vec.at(1) = " << vec.at(1) << std::endl; // 输出第二个元素
// 获取 vector 的大小
std::cout << "vector size = " << vec.size() << std::endl;
// 遍历 vector 中的元素
for (size_t i = 0; i < vec.size(); ++i) {
std::cout << vec[i] << " ";
}
std::cout << std::endl;
// 使用范围 for 循环遍历
for (const auto& element : vec) {
std::cout << element << " ";
}
std::cout << std::endl;
// 删除最后一个元素
vec.pop_back();
// 插入元素
vec.insert(vec.begin() + 1, 4); // 在位置 1 插入元素 4
// 删除元素
vec.erase(vec.begin() + 2); // 删除位置 2 的元素
return 0;
}
2. list
list
是一个双向链表,支持快速插入和删除操作。
#include <iostream>
#include <list>
int main() {
// 创建一个空的 int 类型的 list
std::list<int> lst;
// 向 list 中添加元素
lst.push_back(1); // 在 list 的末尾添加元素 1
lst.push_front(2); // 在 list 的开头添加元素 2
// 遍历 list 中的元素
for (const auto& element : lst) {
std::cout << element << " ";
}
std::cout << std::endl;
// 在 list 中插入元素
auto it = std::next(lst.begin()); // 获取第二个元素的迭代器
lst.insert(it, 3); // 在第二个元素前插入元素 3
// 删除元素
lst.erase(lst.begin()); // 删除第一个元素
return 0;
}
3. map
map
是一个关联容器,存储键值对,并根据键自动排序。
#include <iostream>
#include <map>
#include <string>
int main() {
// 创建一个 string 到 int 的 map
std::map<std::string, int> myMap;
// 插入键值对
myMap["one"] = 1; // 插入键 "one" 和值 1
myMap["two"] = 2; // 插入键 "two" 和值 2
// 使用 insert 插入键值对
myMap.insert(std::make_pair("three", 3));
// 查找元素
auto it = myMap.find("two");
if (it!= myMap.end()) {
std::cout << "Found: " << it->first << " -> " << it->second << std::endl;
}
// 遍历 map 中的元素
for (const auto& pair : myMap) {
std::cout << pair.first << " -> " << pair.second << std::endl;
}
// 删除元素
myMap.erase("one");
return 0;
}
二、迭代器(Iterators)
迭代器用于遍历容器中的元素。
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// 获取 vector 的开始和结束迭代器
std::vector<int>::iterator itBegin = vec.begin();
std::vector<int>::iterator itEnd = vec.end();
// 使用迭代器遍历
for (std::vector<int>::iterator it = itBegin; it!= itEnd; ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
// 使用 const 迭代器遍历
std::vector<int>::const_iterator citBegin = vec.cbegin();
std::vector<int>::const_iterator citEnd = vec.cend();
for (std::vector<int>::const_iterator cit = citBegin; cit!= citEnd; ++cit) {
std::cout << *cit << " ";
}
std::cout << std::endl;
return 0;
}
三、算法(Algorithms)
STL 提供了许多通用算法,例如 sort
、find
等。
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
int main() {
std::vector<int> vec = {3, 1, 4, 1, 5, 9};
// 使用 sort 算法对 vector 排序
std::sort(vec.begin(), vec.end());
// 使用 find 算法查找元素
auto it = std::find(vec.begin(), vec.end(), 5);
if (it!= vec.end()) {
std::cout << "Found 5 at position " << std::distance(vec.begin(), it) << std::endl;
}
// 使用 accumulate 算法求和
int sum = std::accumulate(vec.begin(), vec.end(), 0);
std::cout << "Sum = " << sum << std::endl;
return 0;
}
四、函数对象(Function Objects)
函数对象是可以像函数一样被调用的对象,通常是类的对象,并重载了 operator()
。
#include <iostream>
#include <vector>
#include <algorithm>
// 定义一个函数对象
class GreaterThan {
public:
bool operator()(int value) const {
return value > 5;
}
};
int main() {
std::vector<int> vec = {3, 1, 4, 1, 5, 9};
// 使用函数对象和 count_if 算法统计大于 5 的元素的数量
GreaterThan gt;
int count = std::count_if(vec.begin(), vec.end(), gt);
std::cout << "Count of elements greater than 5: " << count << std::endl;
return 0;
}
五、set
set
是一个存储唯一元素的容器,元素会自动排序。
#include <iostream>
#include <set>
int main() {
std::set<int> mySet;
// 插入元素
mySet.insert(3);
mySet.insert(1);
mySet.insert(4);
mySet.insert(1); // 重复元素不会被插入
// 遍历 set 中的元素
for (const auto& element : mySet) {
std::cout << element << " ";
}
std::cout << std::endl;
// 查找元素
if (mySet.count(4) > 0) {
std::cout << "4 is in the set" << std::endl;
}
return 0;
}
六、stack
stack
是一个后进先出(LIFO)的容器适配器。
#include <iostream>
#include <stack>
int main() {
std::stack<int> stk;
// 压入元素
stk.push(1);
stk.push(2);
stk.push(3);
// 查看栈顶元素
std::cout << "Top element: " << stk.top() << std::endl;
// 弹出元素
stk.pop();
return 0;
}
七、queue
queue
是一个先进先出(FIFO)的容器适配器。
#include <iostream>
#include <queue>
int main() {
std::queue<int> que;
// 入队元素
que.push(1);
que.push(2);
que.push(3);
// 查看队首元素
std::cout << "Front element: " << que.front() << std::endl;
// 出队元素
que.pop();
return 0;
}
八、priority_queue
priority_queue
是一个优先队列,元素按优先级排序。
#include <iostream>
#include <queue>
int main() {
std::priority_queue<int> pq;
// 入队元素
pq.push(3);
pq.push(1);
pq.push(5);
// 查看队首元素(最大值)
std::cout << "Top element: " << pq.top() << std::endl;
// 出队元素
pq.pop();
return 0;
}
这些示例涵盖了 C++ STL 的几个主要部分,你可以根据自己的需求进行扩展和深入学习。你是在学习 STL 的过程中遇到什么困难了吗 还是想要更深入地了解某个特定的 STL 组件呢