C++ 标准库

C++标准库(C++ Standard Library)是C++语言的一部分,提供了一组丰富的工具和组件,涵盖了各种编程需求。标准库由以下几个主要部分组成:

1.标准模板库(STL)

STL提供了常用的数据结构和算法,分为以下几个部分:

容器

1
2
3
4
5
6
7
8
9
10
11
12
13
<array>: 定长数组容器
<vector>: 动态数组容器
<deque>: 双端队列容器
<list>: 双向链表容器
<forward_list>: 单向链表容器
<stack>: 栈容器适配器
<queue>: 队列容器适配器
<priority_queue>: 优先队列容器适配器
<set>: 集合容器(基于平衡二叉树)
<unordered_set>: 无序集合容器(基于哈希表)
<map>: 映射容器(键值对,基于平衡二叉树)
<unordered_map>: 无序映射容器(基于哈希表)
<bitset>: 二进制位容器

迭代器

1
<iterator>: 迭代器

算法

1
<algorithm>: 常用算法(如排序、查找等)

2.输入输出

1
2
3
4
5
6
<iostream>: 标准输入输出流
<fstream>: 文件输入输出流
<sstream>: 字符串流
<iomanip>: 输入输出流格式化
<cstdio>: C 风格输入输出
<cstdint>: 定长整数类型

3.函数对象和绑定

1
<functional>: 定义函数对象及相关工具

4.数学和数值运算

1
2
3
4
<numeric>: 数值操作(如累计、乘积等)
<complex>: 复数运算
<valarray>: 数组类及相关操作
<cmath>: 数学函数

5.字符串和正则表达式

1
2
<string>: 标准字符串类
<regex>: 正则表达式

6.时间和日期

1
2
<ctime>: 时间处理
<chrono>: 时间库

7.多线程和并发

1
2
3
4
5
<thread>: 多线程支持
<mutex>: 互斥量
<condition_variable>: 条件变量
<future>: 异步编程支持
<atomic>: 原子操作

8.内存管理

1
2
<memory>: 智能指针及动态内存管理
<new>: 动态内存分配

9.异常处理

1
2
<exception>: 异常处理基类及相关工具
<stdexcept>: 常用异常类(如 std::runtime_error 等)

10.其他工具

1
2
3
4
5
6
7
8
9
10
11
<utility>: 通用工具(如 std::pair 和 std::move 等)
<random>: 随机数生成
<locale>: 本地化支持
<codecvt>: 字符编码转换
<cassert>: 断言
<cctype>: 字符处理
<cstring>: 字符串处理
<cwchar>: 宽字符处理
<climits>: 数值极限
<cfloat>: 浮点极限
<cstdlib>: 常用工具(如 std::rand 和 std::abs 等)

一、include <functional>

1.1. 标准函数对象

<functional> 提供了一组预定义的函数对象(也称为仿函数,它的行为类似于函数,可以像调用函数一样调用它),主要用于函数式编程和容器操作。这些函数对象都在 std 命名空间中。

算术操作

  • std::plus: 加法
  • std::minus: 减法
  • std::multiplies: 乘法
  • std::divides: 除法
  • std::modulus: 取模
  • std::negate: 取负

用法示例:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <functional>

int main() {
std::plus<int> add;
std::cout << "3 + 4 = " << add(3, 4) << std::endl;

std::negate<int> negate;
std::cout << "Negate 5 = " << negate(5) << std::endl;

return 0;
}

比较操作

  • std::equal_to: 等于
  • std::not_equal_to: 不等于
  • std::greater: 大于
  • std::less: 小于
  • std::greater_equal: 大于等于
  • std::less_equal: 小于等于

逻辑操作

  • std::logical_and: 逻辑与
  • std::logical_or: 逻辑或
  • std::logical_not: 逻辑非

1.2. 函数对象包装器

std::function

  • 是一个通用的、多态的函数包装器,可以存储可调用对象(函数、函数指针、lambda 表达式、函数对象等)。
  • 用法示例:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #include <iostream>
    #include <functional>

    void printMessage(const std::string& message) {
    std::cout << message << std::endl;
    }

    int main() {
    std::function<void(const std::string&)> func = printMessage;
    func("Hello, <functional>!");
    return 0;
    }

std::bind

  • 创建一个新的函数对象,将部分参数绑定到已有函数或可调用对象上。
  • 用法示例:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #include <iostream>
    #include <functional>

    void greet(const std::string& name, const std::string& greeting) {
    std::cout << greeting << ", " << name << "!" << std::endl;
    }

    int main() {
    auto sayHello = std::bind(greet, std::placeholders::_1, "Hello");
    sayHello("Alice"); // 输出:Hello, Alice!
    return 0;
    }

std::placeholders

  • 用于占位符(_1, _2, …),在使用 std::bind 时指定参数的位置。

1.3. 哈希支持

std::hash

  • 用于生成哈希值的函数对象,常用于哈希容器如 std::unordered_mapstd::unordered_set
  • 示例:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #include <iostream>
    #include <functional>
    #include <string>

    int main() {
    std::hash<std::string> hashFunc;
    std::string key = "hello";
    std::cout << "Hash value for '" << key << "': " << hashFunc(key) << std::endl;

    return 0;
    }

1.4. 常见使用场景

  1. 函数回调:用 std::function 包装回调函数,方便传递和调用。
  2. 绑定参数:使用 std::bind 将部分参数绑定,生成更简单的函数接口。
  3. 容器操作:结合算法(如 std::sort)与标准函数对象(如 std::less)实现复杂排序。

后面碰到了再学习。。。