崂山区城市规划建设局网站,推广网络网站,广告策划书案例完整版,网站建设的固定资产包括哪些std 是 C 标准库的命名空间#xff0c;包含了大量标准的 C 类、函数和对象。这些类和函数提供了广泛的功能#xff0c;包括输入输出、容器、算法、字符串处理等。
通常#xff0c;为了使用标准库中的对象和函数#xff0c;需在代码中包含相应的头文件#xff0c;比如 #in…std 是 C 标准库的命名空间包含了大量标准的 C 类、函数和对象。这些类和函数提供了广泛的功能包括输入输出、容器、算法、字符串处理等。
通常为了使用标准库中的对象和函数需在代码中包含相应的头文件比如 #include iostream。然后你就可以通过 std:: 前缀来使用其中的功能比如 std::cout、std::cin、std::endl 等。
这种用法有助于防止命名冲突因为 C 中可能会有多个库提供相同的名称。使用命名空间可以明确指定要使用的是标准库中的功能而不是其他地方定义的同名功能。
C 标准库中常见的类、函数等
1. 类 - std::string: 字符串处理类。 - std::vector: 动态数组容器类。 - std::map、std::unordered_map: 键值对映射容器类。 - std::fstream: 文件输入输出类。 - std::deque: 双端队列容器类。 - std::set、std::unordered_set: 集合容器类。 - std::stack、std::queue: 栈和队列容器适配器类。 - std::stringstream: 字符串流类。
2. 函数 - std::cout、std::cerr: 控制台输出函数。 - std::cin: 控制台输入函数。 - std::sort: 容器排序函数。 - std::find: 容器查找函数。 - std::max、std::min: 返回两个值中较大或较小的值。 - std::accumulate: 容器元素累加函数。 - std::copy: 复制范围内元素到另一个范围函数。 - std::transform: 容器元素转换函数。 - std::regex_search: 正则表达式搜索函数。 - std::regex_match: 正则表达式匹配函数。 - std::regex_replace: 正则表达式替换函数。
3. 对象 - std::endl: 换行并刷新输出流对象。 - std::numeric_limits: 数值类型极限值信息对象。 - std::allocator: 动态内存分配器对象。 - std::cin.eof(): 输入流对象函数检查是否达到文件结束。 - std::nothrow: 内存分配失败时返回空指针而不抛出异常的对象。 - std::random_device: 真随机数生成对象。 - std::locale: 控制 C 标准库本地化行为的对象。
这些类、函数和对象提供了丰富的功能覆盖了输入输出、容器、算法、字符串处理、正则表达式等多个方面为 C 程序员提供了强大的工具可用于各种类型的应用开发。
------------
std::regex 是 C 标准库中提供的用于处理正则表达式的类。正则表达式是一种强大的模式匹配工具它可以用于在字符串中进行复杂的搜索、替换等操作。std::regex 类提供了一种方式来创建、编译和使用正则表达式。
下面是 std::regex 类的一些重要成员函数和用法
1. 构造函数 - explicit regex(const char* fmt, flag_type flags std::regex_constants::ECMAScript) - explicit regex(const std::string fmt, flag_type flags std::regex_constants::ECMAScript) 这些构造函数用于创建 std::regex 对象接受一个正则表达式字符串作为参数并可选择地指定匹配标志。
2. 成员函数 match() 和 search() - bool match(const std::string s, std::regex_constants::match_flag_type flags std::regex_constants::match_default) const - bool search(const std::string s, std::regex_constants::match_flag_type flags std::regex_constants::match_default) const 这两个成员函数分别用于在字符串中进行完全匹配match()和部分匹配search()。它们接受一个待匹配的字符串作为参数并可选择地指定匹配标志。
3. 替换函数 std::regex_replace() - std::string regex_replace(InputIt first, InputIt last, const std::regex re, const std::string fmt, std::regex_constants::match_flag_type flags std::regex_constants::match_default) 这个函数用于在范围 [first, last) 中搜索并替换满足正则表达式 re 的部分。替换的方式由参数 fmt 指定。
4. 正则表达式的语法 std::regex 支持多种正则表达式的语法包括 ECMAScript、basic、extended 等等。你可以通过设置不同的标志来指定使用的语法。常见的标志包括 - std::regex_constants::ECMAScript使用 ECMAScript 语法。 - std::regex_constants::basic使用基本正则表达式语法。 - std::regex_constants::extended使用扩展正则表达式语法。
这些只是 std::regex 类的一些常用成员函数和用法。借助这些函数可方便地在字符串中进行正则表达式的搜索、替换等操作实现了复杂文本处理的功能。
#include iostream
#include regex
#include stringint main() {// 原始字符串std::string text Hello, world!;// 定义正则表达式模式std::regex pattern(world);// 在文本中搜索模式if (std::regex_search(text, pattern)) {std::cout 在文本中找到了匹配的模式 std::endl;}else {std::cout 未找到匹配的模式 std::endl;}return 0;
}/*std::regex表示一个正则表达式对象。 std::smatch保存匹配结果的容器可以通过 std::regex_match 或 std::regex_search 函数填充。 std::regex_match用于检查整个字符串是否与正则表达式匹配。 std::regex_search在输入字符串中搜索与正则表达式匹配的内容。 std::regex_replace用于在字符串中执行正则表达式替换操作。 std::regex_iterator用于迭代一个字符串中所有与正则表达式匹配的子串。 std::regex_token_iterator用于迭代一个字符串中与正则表达式匹配的子串及其非匹配部分。*/
#include iostream
#include regexint main() {std::string text Hello, my email is exampleemail.com and my phone number is 123-456-7890.;std::regex emailRegex([a-zA-Z0-9._%-][a-zA-Z0-9.-]\\.[a-zA-Z]{2,});std::regex phoneRegex(\\d{3}-\\d{3}-\\d{4});// Match emailstd::smatch emailMatch;if (std::regex_search(text, emailMatch, emailRegex)) {std::cout Email found: emailMatch.str() std::endl;}// Match phone numberstd::smatch phoneMatch;if (std::regex_search(text, phoneMatch, phoneRegex)) {std::cout Phone number found: phoneMatch.str() std::endl;}// Replace phone numberstd::string newText std::regex_replace(text, phoneRegex, XXX-XXX-XXXX);std::cout Text with phone number replaced: newText std::endl;return 0;
}/*std::regex表示一个正则表达式对象。
std::smatch保存匹配结果的容器可以通过 std::regex_match 或 std::regex_search 函数填充。
std::regex_match用于检查整个字符串是否与正则表达式匹配。
std::regex_search在输入字符串中搜索与正则表达式匹配的内容。
std::regex_replace用于在字符串中执行正则表达式替换操作。
std::regex_iterator用于迭代一个字符串中所有与正则表达式匹配的子串。
std::regex_token_iterator用于迭代一个字符串中与正则表达式匹配的子串及其非匹配部分。*/