C++标准库(Standard Library)是C++编程语言的重要组成部分,它提供了大量的函数、类和模板,用于各种常见的编程任务,如输入输出、字符串处理、数据结构和算法等。C++标准库使得编写高效、可维护和可移植的程序变得更加容易。

以下是C++标准库的一些主要组件和头文件:

基础输入输出流

  • <iostream>: 提供输入输出流,如std::cin, std::cout, std::cerr

    cpp
    1
    2
    #include <iostream>
    std::cout << "Hello, World!" << std::endl;
  • <iomanip>: 提供输入输出流的格式控制,如设置宽度、填充字符等。

    cpp
    1
    2
    #include <iomanip>
    std::cout << std::setw(10) << 42 << std::endl;
  • <fstream>: 提供文件输入输出流,如std::ifstream, std::ofstream

    cpp
    1
    2
    3
    #include <fstream>
    std::ofstream outfile("example.txt");
    outfile << "Writing to a file" << std::endl;

容器

  • <vector>: 动态数组,提供随机访问和动态调整大小。

    cpp
    1
    2
    #include <vector>
    std::vector<int> v = {1, 2, 3, 4};
  • <list>: 双向链表,提供高效的插入和删除操作。

    cpp
    1
    2
    #include <list>
    std::list<int> lst = {1, 2, 3, 4};
  • <deque>: 双端队列,提供快速的头尾插入和删除操作。

    cpp
    1
    2
    #include <deque>
    std::deque<int> dq = {1, 2, 3, 4};
  • <array>: 定长数组,提供数组的基本功能。

    cpp
    1
    2
    #include <array>
    std::array<int, 4> arr = {1, 2, 3, 4};
  • <set>: 集合,提供唯一性和有序性。

    cpp
    1
    2
    #include <set>
    std::set<int> s = {1, 2, 3, 4};
  • <map>: 映射,提供键值对存储和快速查找。

    cpp
    1
    2
    #include <map>
    std::map<int, std::string> m = {{1, "one"}, {2, "two"}};

算法

  • <algorithm>: 提供一系列通用算法,如排序、查找、变换等。

    cpp
    1
    2
    3
    #include <algorithm>
    std::vector<int> v = {4, 2, 3, 1};
    std::sort(v.begin(), v.end());
  • <numeric>: 提供一些常用的数值算法,如累加、内积等。

    cpp
    1
    2
    3
    #include <numeric>
    std::vector<int> v = {1, 2, 3, 4};
    int sum = std::accumulate(v.begin(), v.end(), 0);

字符串和正则表达式

  • <string>: 提供字符串类std::string和相关函数。

    cpp
    1
    2
    #include <string>
    std::string str = "Hello, World!";
  • <regex>: 提供正则表达式的支持。

    cpp
    1
    2
    3
    #include <regex>
    std::regex pattern("[a-z]+");
    bool match = std::regex_match("hello", pattern);

智能指针

  • <memory>: 提供智能指针,如std::shared_ptr, std::unique_ptr, std::weak_ptr
    cpp
    1
    2
    #include <memory>
    std::shared_ptr<int> p = std::make_shared<int>(42);

线程和并发

  • <thread>: 提供线程支持。

    cpp
    1
    2
    3
    4
    5
    6
    #include <thread>
    void thread_function() {
    std::cout << "Hello from thread!" << std::endl;
    }
    std::thread t(thread_function);
    t.join();
  • <mutex>: 提供互斥锁和同步原语。

    cpp
    1
    2
    #include <mutex>
    std::mutex mtx;
  • <future>: 提供异步操作和期望值。

    cpp
    1
    2
    3
    #include <future>
    std::future<int> fut = std::async(std::launch::async, []() { return 42; });
    int result = fut.get();

时间和日期

  • <chrono>: 提供时间和日期处理。
    cpp
    1
    2
    3
    4
    5
    #include <chrono>
    auto start = std::chrono::high_resolution_clock::now();
    // some operations
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();

数学和随机数

  • <cmath>: 提供数学函数,如sqrt, pow, sin, cos等。

    cpp
    1
    2
    #include <cmath>
    double result = std::sqrt(16.0);
  • <random>: 提供随机数生成。

    cpp
    1
    2
    3
    4
    5
    #include <random>
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(1, 6);
    int random_number = dis(gen);

异常处理

  • <exception>: 提供异常类和处理函数。
    cpp
    1
    2
    3
    4
    5
    6
    #include <exception>
    try {
    throw std::runtime_error("An error occurred");
    } catch (const std::exception& e) {
    std::cout << "Caught exception: " << e.what() << std::endl;
    }

这些只是C++标准库的一部分。完整的C++标准库非常庞大,涵盖了从基础到高级的各种功能模块。学习和使用这些标准库可以大大提高你的编程效率和代码质量。