C++标准库(std)用法解读

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