C++标准库(std)详解
C++标准库(std)详解——目录
- C++标准库(`std`)详解
- 一、命名空间(`namespace`)
- 二、主要组件
- 1. 输入输出流(`<iostream>`)
- 2. 字符串处理(`<string>`)
- 3. STL容器(`<vector>`,` <list>`,` <map>`, `<set>`等)
- 4. 算法(`<algorithm>`)
- 5. 数值处理(`<numeric>`)
- 6. 函数对象与可调用对象(`<functional>`)
- 7. 异常处理(`<exception>`)
- 8. 时间日期(`<chrono>`)
- 9. 文件系统(`<filesystem>`)
- 10. 线程支持(`<thread>`,` <mutex>`,` <condition_variable>`等)
- 11. 正则表达式(`<regex>`)
C++标准库(std
)详解
C++是一种功能强大的编程语言,其标准库(std
)提供了丰富的功能和工具,帮助开发者高效地进行编程。本文将详细介绍C++标准库的主要组成部分及其使用方法。
一、命名空间(namespace
)
在C++中,标准库的所有内容都定义在std
命名空间中。这是为了避免与用户自定义的函数或类名发生冲突。使用std
命名空间有两种主要方式:
- 显式指定:每次使用标准库中的函数或类时,都加上
std::
前缀,例如std::cout
、std::vector
。 - 使用声明:通过
using
关键字引入特定的名称,例如using std::cout;
,这样在当前作用域内就可以直接使用cout
而无需加std::
前缀。
二、主要组件
1. 输入输出流(<iostream>
)
<iostream>
头文件提供了用于控制台输入输出的功能,主要包括:
std::cin
:标准输入流,通常用于从键盘读取输入。std::cout
:标准输出流,用于向控制台输出信息。std::cerr
:标准错误流,用于输出错误信息,通常不带缓冲。std::clog
:标准日志流,用于输出日志信息,通常带缓冲。
示例代码:
#include <iostream>int main() {std::cout << "Hello, World!" << std::endl;int a;std::cin >> a;std::cout << "You entered: " << a << std::endl;return 0;
}
2. 字符串处理(<string>
)
<string>
头文件提供了std::string
类,用于处理字符串。常用操作包括:
std::string
:表示字符串对象。std::getline
:从输入流中读取一行字符串。str.size()
:返回字符串的长度。str.substr(pos, len)
:获取子字符串。str1 + str2
:字符串拼接。
示例代码:
#include <iostream>
#include <string>int main() {std::string str1 = "Hello";std::string str2 = "World";std::string result = str1 + ", " + str2 + "!";std::cout << result << std::endl;return 0;
}
3. STL容器(<vector>
, <list>
, <map>
, <set>
等)
C++标准库提供了多种容器类,用于存储和管理数据。常见的容器包括:
std::vector
:动态数组,支持随机访问。std::list
:双向链表,支持快速插入和删除。std::map
:关联容器,存储键值对,按键排序。std::set
:集合容器,存储唯一元素,自动排序。
示例代码(使用std::vector
):
#include <iostream>
#include <vector>int main() {std::vector<int> numbers = {1, 2, 3, 4, 5};for (int num : numbers) {std::cout << num << " ";}std::cout << std::endl;return 0;
}
4. 算法(<algorithm>
)
<algorithm>
头文件提供了许多通用算法,可以应用于各种容器。常用算法包括:
std::sort
:对容器中的元素进行排序。std::find
:查找容器中的某个元素。std::count
:统计容器中满足条件的元素个数。std::copy
:将一个容器的元素复制到另一个容器。
示例代码:
#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> numbers = {5, 3, 1, 4, 2};std::sort(numbers.begin(), numbers.end());for (int num : numbers) {std::cout << num << " ";}std::cout << std::endl;return 0;
}
5. 数值处理(<numeric>
)
<numeric>
头文件提供了一些数学运算相关的函数,例如:
std::accumulate
:计算容器中元素的累加和。std::inner_product
:计算两个容器的内积。
示例代码:
#include <iostream>
#include <vector>
#include <numeric>int main() {std::vector<int> numbers = {1, 2, 3, 4, 5};int sum = std::accumulate(numbers.begin(), numbers.end(), 0);std::cout << "Sum: " << sum << std::endl;return 0;
}
6. 函数对象与可调用对象(<functional>
)
<functional>
头文件提供了函数对象和可调用对象的包装器,例如:
std::function
:通用的可调用对象包装器,可以存储任何可调用对象(如函数、Lambda表达式、函数对象)。std::bind
:绑定函数参数,生成新的可调用对象。
示例代码:
#include <iostream>
#include <functional>void printSquare(int x) {std::cout << x * x << std::endl;
}int main() {std::function<void(int)> func = printSquare;func(5); // 输出 25return 0;
}
7. 异常处理(<exception>
)
<exception>
头文件提供了标准异常类,用于处理程序中的异常情况。常见的异常类包括:
std::exception
:基类,提供what()
方法返回异常描述。std::runtime_error
:运行时错误异常。std::logic_error
:逻辑错误异常。
示例代码:
#include <iostream>
#include <exception>int main() {try {throw std::runtime_error("An error occurred");} catch (const std::exception& e) {std::cout << "Caught exception: " << e.what() << std::endl;}return 0;
}
8. 时间日期(<chrono>
)
<chrono>
头文件提供了处理时间和日期的功能,例如:
std::chrono::system_clock
:系统时钟,用于获取当前时间。std::chrono::duration
:表示时间段。std::chrono::time_point
:表示时间点。
示例代码:
#include <iostream>
#include <chrono>
#include <thread>int main() {auto now = std::chrono::system_clock::now();std::cout << "Current time: " << std::chrono::system_clock::to_time_t(now) << std::endl;std::this_thread::sleep_for(std::chrono::seconds(1));return 0;
}
9. 文件系统(<filesystem>
)
<filesystem>
头文件提供了操作文件系统的功能,例如:
std::filesystem::path
:表示文件路径。std::filesystem::exists
:检查文件是否存在。std::filesystem::create_directory
:创建目录。std::filesystem::remove
:删除文件或目录。
示例代码:
#include <iostream>
#include <filesystem>namespace fs = std::filesystem;int main() {fs::path p = "example.txt";if (fs::exists(p)) {std::cout << p << " exists" << std::endl;} else {std::cout << p << " does not exist" << std::endl;}return 0;
}
10. 线程支持(<thread>
, <mutex>
, <condition_variable>
等)
C++11引入了多线程支持,相关头文件包括:
std::thread
:表示线程对象。std::mutex
:互斥锁,用于保护共享数据。std::condition_variable
:条件变量,用于线程间同步。
示例代码:
#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void printMessage(const std::string& message) {std::lock_guard<std::mutex> lock(mtx);std::cout << message << std::endl;
}int main() {std::thread t1(printMessage, "Hello from thread 1");std::thread t2(printMessage, "Hello from thread 2");t1.join();t2.join();return 0;
}
11. 正则表达式(<regex>
)
<regex>
头文件提供了正则表达式相关的类和函数,用于字符串的模式匹配和搜索。常用功能包括:
std::regex
:表示正则表达式对象。std::regex_match
:检查整个字符串是否匹配正则表达式。std::regex_search
:搜索字符串中与正则表达式匹配的部分。std::regex_replace
:替换字符串中与正则表达式匹配的部分。
示例代码:
#include <iostream>
#include <regex>
#include <string>int main() {std::string text = "Hello, World!";std::regex pattern("\\bWorld\\b"); // 匹配单词 "World"if (std::regex_search(text, pattern)) {std::cout << "Match found!" << std::endl;} else {std::cout << "No match found." << std::endl;}return 0;
}