标准模板库 STL

翻译整理自 cppreference - Containers 与 Algorithms。

1. 容器分类

类型 容器
顺序 vector deque list forward_list array
关联 set map multiset multimap
无序关联 unordered_set unordered_map ...
适配器 stack queue priority_queue
字符串 string wstring u16string u32string

2. vector 动态数组

#include <vector>
std::vector<int> v{1, 2, 3};
v.push_back(4);
v.emplace_back(5);                  // 原地构造,更高效
v.size();
v[0]; v.at(0);
v.front(); v.back();
v.pop_back();
v.clear();
v.reserve(100);                     // 预分配,避免重分配

for (int x : v) std::cout << x;

v.erase(v.begin() + 1);
v.insert(v.begin(), 0);

3. list 双向链表

#include <list>
std::list<int> l{1, 2, 3};
l.push_front(0);
l.push_back(4);
l.sort();
l.unique();

4. map / unordered_map

#include <map>
std::map<std::string, int> m;
m["Alice"] = 90;
m.insert({"Bob", 80});

if (m.count("Alice")) { ... }

for (const auto& [k, v] : m) {       // C++17 结构化解构
    std::cout << k << ":" << v;
}

auto it = m.find("Alice");
if (it != m.end()) it->second = 95;

unordered_map 哈希实现,平均 O(1),但迭代顺序不固定:

#include <unordered_map>
std::unordered_map<std::string, int> hm;

5. set / multiset

std::set<int> s{3, 1, 2};
s.insert(4);
s.erase(1);
s.count(2);
for (int x : s) std::cout << x;       // 2 3 4(有序)

6. stack / queue / priority_queue

#include <stack>
std::stack<int> st; st.push(1); st.top(); st.pop();

#include <queue>
std::queue<int> q; q.push(1); q.front(); q.pop();

std::priority_queue<int> pq;          // 默认大顶堆
pq.push(3); pq.push(1); pq.top();    // 3

7. 迭代器

std::vector<int> v{1, 2, 3};
auto it = v.begin();
*it;            // 1
++it;
it != v.end();

// 反向
for (auto rit = v.rbegin(); rit != v.rend(); ++rit) { ... }

迭代器分类

  • 输入 / 输出
  • 前向 forward_iterator
  • 双向 bidirectional_iterator
  • 随机访问 random_access_iterator
  • C++20 起:连续 contiguous_iterator

8. 算法库

#include <algorithm>

std::vector<int> v{3, 1, 4, 1, 5};

std::sort(v.begin(), v.end());
std::reverse(v.begin(), v.end());
std::find(v.begin(), v.end(), 3);
std::count(v.begin(), v.end(), 1);
std::min_element(v.begin(), v.end());
std::max_element(v.begin(), v.end());
std::accumulate(v.begin(), v.end(), 0);
std::transform(v.begin(), v.end(), v.begin(),
               [](int x) { return x * 2; });
std::copy(v.begin(), v.end(), std::back_inserter(other));
std::for_each(v.begin(), v.end(), [](int x){ std::cout << x; });

9. C++20 ranges(更现代)

#include <ranges>
namespace v = std::views;

auto r = v
    | v::filter([](int x){ return x > 0; })
    | v::transform([](int x){ return x * 2; });

for (int x : r) std::cout << x;

std::ranges::sort(v);
auto even = v | v::filter([](int x){ return x % 2 == 0; });

10. 字符串

#include <string>
std::string s = "Hello";
s.length();
s += ", world";
s.substr(0, 3);
s.find("world");
std::stoi("42");
std::to_string(42);

11. 数值与函数对象

#include <numeric>
std::iota(v.begin(), v.end(), 1);     // 1,2,3,...
std::reduce(v.begin(), v.end());       // C++17 起可并行

#include <functional>
std::function<int(int, int)> f = std::plus<int>();
std::sort(v.begin(), v.end(), std::greater<int>());

12. 工具

  • <tuple>:元组
  • <utility>:pair、move、forward
  • <optional> (C++17):可能为空的值
  • <variant> (C++17):类型安全的联合
  • <any> (C++17):任意类型值
  • <chrono>:时间
  • <thread> <mutex> <future>:并发
std::optional<int> parse(const std::string& s);
if (auto v = parse("42")) { use(*v); }

std::variant<int, std::string> v;
v = "hello";
std::get<std::string>(v);
std::visit(visitor, v);

小结

STL 是 C++ 工程化的基石,掌握容器与算法,绝大部分场景都能高效表达。