目录

  1. C++ 标准库概述
  2. 标准库组件
    • 头文件与命名空间
    • 容器(Containers)
    • 迭代器(Iterators)
    • 算法(Algorithms)
    • 输入输出(I/O Streams)
    • 多线程支持(Threading)
    • 智能指针(Smart Pointers)
    • 正则表达式(Regular Expressions)
    • 文件系统(Filesystem)
  3. 标准库主要功能
    • 常用 STL 容器
    • 迭代器及适配器
    • 数值计算与数学函数
    • 时间处理(Chrono)
    • 随机数生成
    • 线程同步与异步编程
  4. C++17/20/23 标准库更新
  5. C++ 标准库常见问题与优化
  6. 参考资料

1. C++ 标准库概述

C++ 标准库(Standard Library)是 C++ 语言的核心组件,提供了一系列通用的功能,包括数据结构、算法、输入输出、并发编程等,极大地提高了开发效率。

标准库的特点:

  • 模块化:由多个头文件组成,可以按需包含。
  • 高效性:提供优化的数据结构和算法。
  • 跨平台:可在不同操作系统上运行。

2. 标准库组件

2.1 头文件与命名空间

C++ 标准库的主要组件位于 std 命名空间内,常见的头文件包括:

头文件主要功能
<iostream>标准输入输出流
<vector>动态数组容器
<map>关联容器
<thread>多线程编程
<memory>智能指针
<chrono>时间处理
<random>随机数生成
<regex>正则表达式
<filesystem>文件系统操作

示例:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3};
    for (int x : v) std::cout << x << " ";
}


2.2 容器(Containers)

标准库容器分为:

  • 顺序容器vectordequelistarray
  • 关联容器setmapunordered_map
  • 适配器stackqueuepriority_queue

示例:

#include <map>
#include <iostream>

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


2.3 迭代器(Iterators)

迭代器用于遍历 STL 容器:

#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {10, 20, 30};
    for (auto it = v.begin(); it != v.end(); ++it) {
        std::cout << *it << " ";
    }
}


2.4 算法(Algorithms)

标准库提供的算法包括排序、查找、变换等:

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

int main() {
    std::vector<int> v = {5, 2, 8, 1};
    std::sort(v.begin(), v.end());
    for (int x : v) std::cout << x << " ";
}


2.5 输入输出(I/O Streams)

标准库的 I/O 相关头文件:

  • iostream:标准输入输出
  • fstream:文件流操作
  • sstream:字符串流

示例:

#include <fstream>
#include <iostream>

int main() {
    std::ofstream file("test.txt");
    file << "Hello, C++!";
    file.close();
}


2.6 多线程支持(Threading)

C++11 引入 thread 进行多线程编程:

#include <thread>
#include <iostream>

void func() { std::cout << "Hello from thread!"; }

int main() {
    std::thread t(func);
    t.join();
}


2.7 智能指针(Smart Pointers)

C++ 标准库提供:

  • unique_ptr(独占管理)
  • shared_ptr(共享管理)
  • weak_ptr(弱引用)

示例:

#include <memory>
#include <iostream>

int main() {
    std::shared_ptr<int> p = std::make_shared<int>(10);
    std::cout << *p;
}


2.8 正则表达式(Regular Expressions)

C++11 引入正则表达式库:

#include <regex>
#include <iostream>

int main() {
    std::regex pattern("[a-z]+");
    std::string text = "hello123";
    std::cout << std::regex_match(text, pattern);
}


2.9 文件系统(Filesystem)

C++17 提供 std::filesystem 进行文件操作:

#include <filesystem>
#include <iostream>

int main() {
    std::filesystem::create_directory("test_dir");
}


3. 标准库主要功能

3.1 时间处理(Chrono)

#include <chrono>
#include <iostream>

int main() {
    auto start = std::chrono::high_resolution_clock::now();
    // 执行代码
    auto end = std::chrono::high_resolution_clock::now();
    std::cout << "执行时间: " << std::chrono::duration<double>(end - start).count() << "s";
}


3.2 随机数生成

#include <random>
#include <iostream>

int main() {
    std::mt19937 gen(std::random_device{}());
    std::uniform_int_distribution<int> dist(1, 100);
    std::cout << dist(gen);
}


3.3 线程同步

#include <mutex>
#include <thread>
#include <iostream>

std::mutex mtx;

void print() {
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Thread safe!\n";
}

int main() {
    std::thread t1(print), t2(print);
    t1.join();
    t2.join();
}


4. C++17/20/23 标准库更新

C++17

  • std::optional
  • std::any
  • std::filesystem
  • std::string_view

C++20

  • std::ranges
  • std::coroutine
  • concepts

C++23

  • std::expected
  • std::flat_map
  • std::print

5. C++ 标准库常见问题与优化

  • STL 容器性能优化
  • 线程安全问题
  • 智能指针的生命周期管理
  • 避免 std::vector<bool> 的特殊实现

6. 参考资料