STL 提供容器、迭代器和算法,是 C++ 高效开发的基础设施。
本节要点
- vector 是动态数组,支持随机访问。
- map 保存有序键值对,unordered_map 基于哈希。
- algorithm 中的 sort、find、transform 非常常用。
代码示例
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
int main() {
std::vector<int> nums = {4, 2, 5, 1, 3};
std::sort(nums.begin(), nums.end());
std::map<std::string, int> scores;
scores["Java"] = 95;
scores["C++"] = 92;
for (const auto &item : scores) {
std::cout << item.first << ": " << item.second << std::endl;
}
return 0;
}