> For the complete documentation index, see [llms.txt](https://docs.1l.lol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.1l.lol/data-structures/stl.md).

# STL 标准库

### STL 是什么

STL（标准模板库）提供现成的**容器、迭代器和算法**，让编程更高效。需要 `#include` 对应头文件。

### 常用容器

| 容器               | 说明             | 头文件         |
| ---------------- | -------------- | ----------- |
| `vector`         | 动态数组           | `<vector>`  |
| `string`         | 字符串            | `<string>`  |
| `pair`           | 二元组            | `<utility>` |
| `set / multiset` | 有序集合（去重 / 不去重） | `<set>`     |
| `map`            | 键值映射（有序）       | `<map>`     |
| `stack`          | 栈              | `<stack>`   |
| `queue`          | 队列             | `<queue>`   |
| `priority_queue` | 优先队列（堆）        | `<queue>`   |
| `deque`          | 双端队列           | `<deque>`   |

```cpp
vector<int> v = {3, 1, 4};
v.push_back(2);              // 追加
cout << v.size();            // 元素个数
sort(v.begin(), v.end());    // 排序

map<string, int> mp;
mp["apple"] = 3;             // 键值对
cout << mp["apple"];         // 3
```

### 迭代器

迭代器是访问容器元素的“指针”：

```cpp
for (auto it = v.begin(); it != v.end(); ++it)
    cout << *it << " ";
for (int x : v) cout << x << " ";   // 范围 for，更简洁
```

### 常用算法（）

| 函数                           | 作用              |
| ---------------------------- | --------------- |
| `sort(begin, end)`           | 排序              |
| `reverse(begin, end)`        | 翻转              |
| `find(begin, end, x)`        | 查找              |
| `lower_bound(begin, end, x)` | 第一个 ≥ x 的位置（有序） |
| `upper_bound(begin, end, x)` | 第一个 > x 的位置（有序） |
| `max(a, b)` / `min(a, b)`    | 取最值             |
| `swap(a, b)`                 | 交换              |

### 小结

先掌握 `vector`、`string`、`pair`、`map`、`set` 和 `sort`，再学 `stack/queue/priority_queue`，是竞赛最常用的组合。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.1l.lol/data-structures/stl.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
