CSP-J/S深度系列之C++语言:第25章 字符串高频操作

B站影视 2024-12-06 23:22 11

摘要:在C++中,std::string类有一个成员函数length,它返回字符串中字符的数量,不包括结尾的空字符(null character)。此外,还有一个成员函数size,其功能和length相同,都是返回字符串的长度。

在C++中,std::string类有一个成员函数length,它返回字符串中字符的数量,不包括结尾的空字符(null character)。此外,还有一个成员函数size,其功能和length相同,都是返回字符串的长度。

#include #include int main { // 创建一个std::string对象并初始化 std::string str = "Hello, World!"; // 使用length函数获取字符串长度 std::size_t length = str.length; // 使用size函数获取字符串长度(与length功能相同) std::size_t size = str.size; // 输出字符串的长度 std::cout

注意,length和size函数返回的是std::size_t类型的值,这是一个无符号整数类型,通常用于表示大小或长度。

2 判断字符串是否为空

在C++中,std::string类提供了empty成员函数,该函数在字符串长度为0时返回true,否则返回false。

#include #include int main { // 创建一个std::string对象并初始化 std::string str(""); // 使用empty检查字符串是否为空 if (str.empty) { std::cout

除此之外,还可以通过求字符串长度是否有效来判定字符串是否为空。也就是size 或 length两个成员函数返回字符串的长度。如果返回的值是0,那么字符串就是空的。

3 字符串间比较

在C++中,std::string类重载了比较运算符,包括:

`bool operator==(const std::string& str) const`: 检查字符串是否等于 `str`。`bool operator!=(const std::string& str) const`: 检查字符串是否不等于 `str`。`bool operator(const std::string& str) const`: 检查字符串是否大于 `str`。`bool operator>=(const std::string& str) const`: 检查字符串是否大于或等于 `str`。`int compare(const std::string& str) const`: 比较字符串与 `str`。

这使得我们可以直接使用比较运算符(如==、!=、、>=)来比较两个字符串对象。

#include #include int main { // 创建两个std::string对象 std::string str1 = "Hello"; std::string str2 = "World"; std::string str3 = "Hello"; // 使用==运算符比较两个字符串是否相等 if (str1 == str2) { std::cout 运算符比较两个字符串(字典序) if (str2 > str1) { std::cout

注意

比较是基于字符的字典序进行的。字符串比较是基于字符的Unicode值进行的,因此,对于非ASCII字符,比较的结果可能会与我们想象的不同,特别是当涉及到不同的字符编码或本地化设置时。

4 字符串查找

在C++中,std::string类提供了几种方法来查找子字符串或字符在字符串中的位置。以下是一些常用的方法:

`find`: 这个函数用于查找子字符串或字符首次出现的位置。如果找到,它返回子字符串或字符的起始位置的索引;如果没有找到,它返回`std::string::npos`。`rfind`: 这个函数与`find`类似,但是它从字符串的末尾开始向前查找。`find_first_of`: 这个函数查找任何一个字符在给定字符集中的首次出现位置。`find_last_of`: 这个函数与`find_first_of`类似,但是它从字符串的末尾开始向前查找。`find_first_not_of`: 这个函数查找第一个不在给定字符集中的字符的位置。`find_last_not_of`: 这个函数与`find_first_not_of`类似,但是它从字符串的末尾开始向前查找。#include #include int main { // 创建一个std::string对象 std::string str = "Hello, World!"; // 使用find查找子字符串 std::size_t found = str.find("World"); if (found != std::string::npos) { std::cout

5 数字转字符串

在C++中,将数字转换为字符串通常使用标准库提供的函数,这些函数以简单直观的方式执行转换。C++11及以上版本,通常使用 std::to_string函数,它接受一个数字作为参数,并返回一个表示该数字的字符串。

#include #include int main { int number = 12345; std::string str = std::to_string(number); // 输出 "12345" std::cout

6 字符串更多操作

构造函数std::string: 创建一个空字符串。std::string(const char* s): 使用 C 字符串 s 创建一个字符串。std::string(const std::string& str): 使用另一个字符串 str 创建一个副本。std::string(size_t n, char c): 创建一个包含 n 个字符 c 的字符串。std::string(const_iterator first, const_iterator last): 使用迭代器 first 和 last 创建一个字符串。赋值操作std::string& operator=(const std::string& str): 将字符串设置为 str。std::string& operator=(const char* s): 将字符串设置为 C 字符串 s。std::string& operator=(char c): 将字符串设置为单个字符 c 的重复。std::string& assign(const std::string& str): 将字符串设置为 str。std::string& assign(const char* s): 将字符串设置为 C 字符串 s。std::string& assign(size_t n, char c): 将字符串设置为 n 个字符 c 的重复。字符串判空操作size_t length const: 返回字符串的长度。size_t size const: 返回字符串的长度(与 length 相同)。bool empty const: 检查字符串是否为空。const char* c_str const: 返回指向正规 C 字符串的指针。const char* data const: 返回指向字符串数据的指针。void clear: 清空字符串。字符串比较bool operator==(const std::string& str) const: 检查字符串是否等于 str。bool operator!=(const std::string& str) const: 检查字符串是否不等于 str。bool operatorbool operatorbool operator>(const std::string& str) const: 检查字符串是否大于 str。bool operator>=(const std::string& str) const: 检查字符串是否大于或等于 str。int compare(const std::string& str) const: 比较字符串与 str。字符串连接和插入std::string& operator+=(const std::string& str): 将字符串 str 连接到当前字符串。std::string& operator+=(const char* s): 将 C 字符串 s 连接到当前字符串。std::string& operator+=(char c): 将字符 c 连接到当前字符串。std::string& append(const std::string& str): 将字符串 str 连接到当前字符串。std::string& append(const char* s): 将 C 字符串 s 连接到当前字符串。std::string& append(size_t n, char c): 将 n 个字符 c 连接到当前字符串。std::string& insert(size_t pos, const std::string& str): 在位置 pos 插入字符串 str。std::string& insert(size_t pos, const char* s): 在位置 pos 插入 C 字符串 s。std::string& insert(size_t pos, size_t n, char c): 在位置 pos 插入 n 个字符 c。字符串查找和替换size_t find(const std::string& str, size_t pos = 0) const: 查找子字符串 str,从位置 pos 开始。size_t find(const char* s, size_t pos = 0) const: 查找 C 字符串 s,从位置 pos 开始。size_t find(char c, size_t pos = 0) const:查找字符 C,从位置 pos 开始。size_t rfind(const string& str, size_t pos = npos) const:查找子字符串 str,从尾部的位置 pos 开始。size_t rfind(const char* s, size_t pos = npos) const:查找 C 字符串 s,从尾部的位置 pos 开始。size_t rfind(char c, size_t pos = npos) const:查找字符 C,从尾部的位置 pos 开始。size_t find_first_of(const string& str, size_t pos = 0) const:查找第一个在给定字符集中的字符串的位置,从位置 pos 开始。size_t find_first_of(const char* s, size_t pos = 0) const:查找第一个在给定字符集中的字符串的位置,从位置 pos 开始。size_t find_first_of(char c, size_t pos = 0) const:查找第一个在给定字符集中的字符的位置,从位置 pos 开始。size_t find_last_of(const string& str, size_t pos = npos) const:从字符串末尾开始反向查找第一个在给定字符集中的字符串的位置,从位置 pos 开始。size_t find_last_of(const char* s, size_t pos = npos) const:从字符串末尾开始反向查找第一个在给定字符集中的字符串的位置,从位置 pos 开始。size_t find_last_of(char c, size_t pos = npos) const:从字符串末尾开始反向查找第一个在给定字符集中的字符的位置,从位置 pos 开始。size_t find_first_not_of(const string& str, size_t pos = 0) const:查找第一个不在给定字符集中的字符串的位置,从位置 pos 开始。size_t find_first_not_of(const char* s, size_t pos = 0) const:查找第一个不在给定字符集中的字符串的位置,从位置 pos 开始。size_t find_first_not_of(char c, size_t pos = 0) const:查找第一个不在给定字符集中的字符的位置,从位置 pos 开始。size_t find_last_not_of(const string& str, size_t pos = npos) const: 从字符串末尾开始反向查找第一个不在给定字符集中的字符串的位置,从位置 pos 开始。size_t find_last_not_of(const char* s, size_t pos = npos) const:从字符串末尾开始反向查找第一个不在给定字符集中的字符串的位置,从位置 pos 开始。size_t find_last_not_of(char c, size_t pos = npos) const:从字符串末尾开始反向查找第一个不在给定字符集中的字符的位置,从位置 pos 开始。string& replace(size_t pos, size_t len, const string& str):替换从指定位置开始的指定长度的子字符串。string& replace(size_t pos, size_t len, const char* s):替换从指定位置开始的指定长度的子字符串。string& replace(size_t pos, size_t len, size_t n, char c):替换从指定位置开始的指定长度的子字符串。

来源:hoogoow

相关推荐