目录

  1. STL 概述
  2. STL 组件介绍
    • 容器(Containers)
    • 算法(Algorithms)
    • 迭代器(Iterators)
    • 仿函数(Functors)
    • 适配器(Adapters)
  3. STL 容器详解
    • 顺序容器vectorlistdequearrayforward_list
    • 关联容器setmultisetmapmultimap
    • 无序容器(哈希容器):unordered_setunordered_mapunordered_multisetunordered_multimap
    • 适配容器stackqueuepriority_queue
  4. STL 迭代器(Iterators)
    • 输入迭代器、输出迭代器、前向迭代器、双向迭代器、随机访问迭代器
    • 常见迭代器操作
  5. STL 算法(Algorithms)
    • 排序、查找、遍历、修改、数值算法
    • sortfindfor_eachaccumulate
  6. STL 仿函数(Functors)
    • 一元仿函数、二元仿函数
    • less<>greater<>plus<>multiplies<>
  7. STL 适配器(Adapters)
    • 容器适配器:stackqueue
    • 迭代器适配器
    • 仿函数适配器
  8. STL 进阶技巧
    • emplace vs insert
    • move 语义与 STL
    • std::bindstd::function
  9. STL 练习与示例代码
  10. 参考资料

1. STL 概述

C++ 标准模板库(STL,Standard Template Library)是一组通用的类和函数模板,提供了一整套高效的数据结构和算法,使 C++ 开发更加便捷。

STL 的主要特点:

  • 模块化:STL 由多个组件组成,包括容器、算法、迭代器等。
  • 高效性:采用泛型编程,提高代码复用性和执行效率。
  • 灵活性:可用于不同类型的数据结构,适用于多种应用场景。

2. STL 组件介绍

2.1 容器(Containers)

STL 容器用于存储和管理数据,包括:

  • 顺序容器(如 vectorlistdeque
  • 关联容器(如 setmap
  • 哈希容器(如 unordered_mapunordered_set

2.2 算法(Algorithms)

STL 提供了一系列常见算法,如:

  • 排序(sort
  • 查找(find
  • 修改(transform
  • 数值计算(accumulate

2.3 迭代器(Iterators)

STL 迭代器用于遍历容器,常见类型包括:

  • 输入迭代器
  • 输出迭代器
  • 前向迭代器
  • 双向迭代器
  • 随机访问迭代器

2.4 仿函数(Functors)

仿函数是一种可以像函数一样调用的对象,通常用于自定义排序规则,如:

struct Compare {
    bool operator()(int a, int b) { return a > b; }
};

2.5 适配器(Adapters)

适配器是 STL 的一种包装器,可用于:

  • 容器适配器(如 stackqueue
  • 迭代器适配器(如 reverse_iterator
  • 仿函数适配器(如 std::bind

3. STL 容器详解

3.1 顺序容器

容器说明
vector<T>动态数组,支持随机访问
list<T>双向链表,支持快速插入删除
deque<T>双端队列,支持前后插入删除
array<T, N>固定大小数组
forward_list<T>单向链表

示例:vector 的基本用法

#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};
    v.push_back(6);  // 添加元素
    for (int x : v) std::cout << x << " ";
}


3.2 关联容器

容器说明
set<T>存储唯一元素,自动排序
multiset<T>允许重复元素
map<K, V>键值对映射,自动排序
multimap<K, V>允许键重复的映射

示例:map 的基本用法

#include <map>
#include <iostream>

int main() {
    std::map<int, std::string> m = {{1, "one"}, {2, "two"}};
    std::cout << m[1];  // 输出 one
}


4. STL 迭代器

迭代器是 STL 容器的核心组件,常见操作:

std::vector<int> v = {10, 20, 30};
std::vector<int>::iterator it = v.begin();
std::cout << *it;  // 输出 10

迭代器类型

迭代器类型说明
输入迭代器只支持读取
输出迭代器只支持写入
前向迭代器支持 ++ 操作
双向迭代器支持 ++ 和 —
随机访问迭代器允许随机访问,如 vector

5. STL 算法

常见算法:

#include <algorithm>
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {3, 1, 4, 1, 5, 9};
    std::sort(v.begin(), v.end());  // 排序
    for (int x : v) std::cout << x << " ";
}


6. STL 仿函数

仿函数是可以像函数一样调用的对象:

#include <functional>
#include <iostream>

struct Multiply {
    int operator()(int x, int y) { return x * y; }
};

int main() {
    Multiply mul;
    std::cout << mul(3, 4);  // 输出 12
}


7. STL 适配器

适配器用于修改 STL 组件的行为,例如:

#include <queue>
#include <iostream>

int main() {
    std::priority_queue<int> pq;
    pq.push(10);
    pq.push(5);
    std::cout << pq.top();  // 输出 10
}


8. STL 进阶技巧

  • emplace vs insert
  • move 语义
  • std::bindstd::function

9. 参考资料