出口外贸营销网站,项目建设情况,网站建设 资讯动态,建设的招标网站C标准库#xff08;std#xff09;详解——目录C标准库#xff08;std#xff09;详解一、命名空间#xff08;namespace#xff09;二、主要组件1. 输入输出流#xff08;iostream#xff09;2. 字符串处理#xff08;string#xff09;3. STL容器标准库std详解——目录C标准库std详解一、命名空间namespace二、主要组件1. 输入输出流iostream2. 字符串处理string3. STL容器vector, list, map, set等4. 算法algorithm5. 数值处理numeric6. 函数对象与可调用对象functional7. 异常处理exception8. 时间日期chrono9. 文件系统filesystem10. 线程支持thread, mutex, condition_variable等11. 正则表达式regexC标准库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 iostreamint 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 stringint 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 vectorint main() {std::vectorint 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 algorithmint main() {std::vectorint 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 numericint main() {std::vectorint 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 functionalvoid printSquare(int x) {std::cout x * x std::endl;
}int main() {std::functionvoid(int) func printSquare;func(5); // 输出 25return 0;
}7. 异常处理exception
exception头文件提供了标准异常类用于处理程序中的异常情况。常见的异常类包括
std::exception基类提供what()方法返回异常描述。std::runtime_error运行时错误异常。std::logic_error逻辑错误异常。
示例代码
#include iostream
#include exceptionint 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 threadint 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 filesystemnamespace 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等
C11引入了多线程支持相关头文件包括
std::thread表示线程对象。std::mutex互斥锁用于保护共享数据。std::condition_variable条件变量用于线程间同步。
示例代码
#include iostream
#include thread
#include mutexstd::mutex mtx;void printMessage(const std::string message) {std::lock_guardstd::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 stringint main() {std::string text Hello, World!;std::regex pattern(\\bWorld\\b); // 匹配单词 Worldif (std::regex_search(text, pattern)) {std::cout Match found! std::endl;} else {std::cout No match found. std::endl;}return 0;
}