编程学习 /C++ /实战应用 /STL 常用容器
⚙️ C++ 实战应用

STL 常用容器

vector、map、set 与算法库的实用写法。

📅 2026-11-23 👁 2 次阅读 🔗 参考 cppreference.com
STL 常用容器示意图

vector 动态数组

#include <vector>
vector<int> v = {1, 2, 3};
v.push_back(4);
for (int x : v) cout << x << " ";
cout << v.size();

map 与 set

#include <map>
#include <set>
map<string, int> m;
m["小明"] = 18;
cout << m["小明"];

set<int> s = {3, 1, 3, 2};
cout << s.size();   // 3,自动去重并排序
STL 容器示意图

配合 #include <algorithm> 可使用 sort、find、reverse 等算法:sort(v.begin(), v.end());