简介
-
标准库
C++ 标准库
C++ 标准库中的 <string>
头文件提供了许多操作字符串的功能,包括创建、操作、处理和管理字符串。使用这个头文件,可以方便地使用字符串并执行各种操作,例如连接字符串、查找子串、截取部分字符串等。
以下是 <string>
标准库中一些常用的类和函数:
字符串类
std::string
: 提供了一个可变长度的字符序列,支持字符串的各种操作和方法,如插入、删除、查找、替换等。
字符串操作函数
std::to_string()
: 将基本数据类型转换为对应的字符串。std::stoi()
,std::stol()
,std::stoll()
,std::stof()
,std::stod()
,std::stold()
: 将字符串转换为对应的数值类型。std::getline()
: 从输入流中读取一行数据到字符串中。
字符串操作方法
size()
,length()
: 返回字符串的长度。append()
,+=
: 在字符串末尾追加内容。insert()
,erase()
,replace()
: 插入、删除、替换子串。substr()
,find()
,rfind()
: 提取子串、查找子串的位置。
字符串比较和转换
compare()
: 比较字符串。toupper()
,tolower()
: 转换为大写或小写。
字符串查询和搜索
find()
,rfind()
,find_first_of()
,find_last_of()
,find_first_not_of()
,find_last_not_of()
: 查找字符串中特定子串或字符的位置。
其他实用函数
c_str()
,data()
: 返回以 null 结尾的 C 风格字符串的指针。empty()
,clear()
,reserve()
,shrink_to_fit()
: 管理字符串的空间和大小。
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, ";
str += "world!"; // 连接字符串
std::cout << str << std::endl;
std::cout << "Length of str: " << str.length() << std::endl;
// 查找子串
if (str.find("world") != std::string::npos) {
std::cout << "Substring 'world' found at position: " << str.find("world") << std::endl;
}
// 提取子串
std::string sub = str.substr(7, 5);
std::cout << "Substring: " << sub << std::endl;
return 0;
}
以上代码演示了一些常见的字符串操作,包括字符串连接、长度获取、查找子串位置、提取子串等。 <string>
头文件提供了丰富的功能,用于处理字符串以及执行各种字符串操作。
C++ 标准库 详解
C++ 标准库中的 <string>
头文件提供了丰富的字符串操作功能,使得在程序中处理字符串变得更加方便和灵活。以下是 <string>
标准库中一些常用的类和函数的详细解释:
std::string
类
std::string
是 C++ 标准库中表示字符串的类。它提供了一系列的方法来操作和管理字符串。这些方法包括:
- 构造函数:
std::string
提供了多种构造函数,允许你用不同的方式初始化字符串。1 2 3
std::string str1; // 初始化为空字符串 std::string str2 = "Hello"; // 用 C 风格字符串初始化 std::string str3(5, 'a'); // 初始化为重复字符的字符串,"aaaaa"
size()
和length()
:返回字符串的长度。1 2
std::string str = "Hello"; std::cout << "Length of str: " << str.length() << std::endl; // 输出 5
- 字符串连接:可以使用
+
运算符或append()
方法来连接字符串。1 2 3 4
std::string str1 = "Hello, "; std::string str2 = "world!"; std::string result = str1 + str2; // 使用 + 运算符 str1.append(str2); // 使用 append() 方法
- 字符串比较:使用
compare()
方法进行字符串的比较。1 2 3
std::string str1 = "apple"; std::string str2 = "banana"; int result = str1.compare(str2); // 返回值 < 0 表示 str1 小于 str2
- 提取子串:使用
substr()
方法提取子串。1 2
std::string str = "Hello, world!"; std::string sub = str.substr(7, 5); // 提取从位置 7 开始的长度为 5 的子串 "world"
- 查找子串:使用
find()
方法查找子串的位置。1 2
std::string str = "Hello, world!"; size_t pos = str.find("world"); // 查找子串 "world" 的位置,返回 7
- 转换为 C 风格字符串:可以使用
c_str()
方法将std::string
转换为以 null 结尾的 C 风格字符串。1 2
std::string str = "Hello"; const char* cstr = str.c_str(); // 返回以 null 结尾的 C 风格字符串
- 其他方法:还有很多其他方法,如
insert()
插入子串、erase()
删除子串、replace()
替换子串等。
其他函数和功能
std::to_string()
:将数值转换为对应的字符串。1 2
int num = 123; std::string str = std::to_string(num); // 将整数转换为字符串 "123"
- 字符串流:
std::stringstream
可以用于字符串和其他数据类型之间的转换。1 2 3 4 5
#include <sstream> int num = 123; std::stringstream ss; ss << num; // 将整数写入字符串流 std::string str = ss.str(); // 从字符串流中获取字符串 "123"
这些只是 <string>
标准库中一部分功能,该库还提供了更多用于字符串处理和操作的功能。使用这些功能,可以更方便地对字符串进行操作、处理和管理。
C++ 标准库 常用函数
C++ 标准库 <string>
中提供了许多常用的函数,用于处理和操作字符串。以下是一些常用的函数及其简要说明:
字符串长度和容量
size()
和length()
: 返回字符串的长度。capacity()
: 返回字符串当前分配的存储容量。
字符串操作
append()
: 在字符串末尾添加内容。insert()
: 在指定位置插入内容。erase()
: 从字符串中删除指定范围的内容。replace()
: 替换指定范围的内容为另一个字符串。substr()
: 提取子串。swap()
: 交换两个字符串的内容。
字符串搜索和查找
find()
和rfind()
: 查找子串的位置,find()
从前往后找,rfind()
从后往前找。find_first_of()
和find_last_of()
: 在字符串中查找字符序列中任意字符的第一个/最后一个匹配位置。find_first_not_of()
和find_last_not_of()
: 查找字符串中第一个/最后一个不在指定字符序列中的字符位置。
字符串比较和处理
compare()
: 比较两个字符串的大小。tolower()
和toupper()
: 将字符串转换为小写或大写。std::stoi()
,std::stol()
,std::stoll()
,std::stof()
,std::stod()
,std::stold()
: 将字符串转换为对应的数值类型。
C 风格字符串处理
c_str()
: 返回以 null 结尾的 C 风格字符串的指针。data()
: 返回指向字符串缓冲区的指针。
字符串空间管理
reserve()
: 设置字符串的预留空间。shrink_to_fit()
: 请求释放未使用的内存空间,将容器的容量减小到与其大小相匹配。
其他实用函数
std::getline()
: 从输入流中读取一行数据到字符串中。std::to_string()
: 将数值转换为对应的字符串。
这些函数提供了对字符串进行各种操作和处理的功能,可以轻松地执行字符串的连接、查找、替换、截取等操作,以及对字符串的大小、长度、容量进行管理。
std::string::size()
std::string::size()
是 C++ 标准库中 std::string
类的成员函数,用于获取字符串的长度,即字符的个数。
这个函数返回一个 size_t
类型的无符号整数,代表字符串中字符的数量。这个字符数量不包括字符串末尾的 null 终止符。
示例用法如下:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
size_t len = str.size(); // 获取字符串的长度
std::cout << "字符串长度为: " << len << std::endl;
return 0;
}
在这个示例中,str.size()
返回的值将是字符串 “Hello, world!” 中字符的总数,包括逗号、空格和感叹号在内。
std::string::length()
std::string::length()
是 C++ 标准库中 std::string
类的成员函数之一,与 size()
函数类似,用于获取字符串的长度,即字符的个数。
这两个函数 size()
和 length()
在 std::string
类中是等价的,都用于返回字符串中字符的数量,不包括末尾的空字符(null terminator)。
示例用法如下:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
size_t len = str.length(); // 或者使用 str.size() 获取字符串的长度
std::cout << "字符串长度为: " << len << std::endl;
return 0;
}
在这个示例中,str.length()
返回的值将是字符串 “Hello, world!” 中字符的总数,包括逗号、空格和感叹号在内。通常来说,length()
和 size()
都可用于获取字符串的长度,并且在 std::string
类中它们的功能是一样的。
std::string::append()
std::string::append()
是 C++ 标准库中 std::string
类的成员函数之一,用于将指定的内容追加到字符串的末尾。
这个函数可以接受多种类型的参数来追加内容,包括:
- 另一个
std::string
对象 - 字符串的一部分(通过指定位置和长度)
- C 风格的字符串
- 字符或者重复的字符
以下是 std::string::append()
函数的几种重载形式:
1
2
3
4
5
std::string& append(const std::string& str); // 将另一个字符串追加到末尾
std::string& append(const std::string& str, size_t pos, size_t len); // 将字符串的一部分追加到末尾
std::string& append(const char* s); // 将 C 风格的字符串追加到末尾
std::string& append(const char* s, size_t n); // 将指定长度的 C 风格字符串追加到末尾
std::string& append(size_t n, char c); // 将字符 c 重复追加 n 次到末尾
以下是一个简单的示例演示了如何使用 std::string::append()
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
int main() {
std::string str = "Hello";
// 追加另一个字符串到末尾
str.append(" world!");
// 追加字符串的一部分到末尾
std::string anotherStr = " This is a test.";
str.append(anotherStr, 0, 11); // 从另一个字符串的指定位置追加一部分内容
// 追加重复的字符到末尾
str.append(3, '!');
std::cout << "结果字符串: " << str << std::endl;
return 0;
}
在这个示例中,std::string::append()
函数被用于将不同类型的内容追加到字符串 str
的末尾,包括另一个字符串、另一个字符串的一部分以及重复的字符。最终输出的字符串将是 “Hello world! This is a test.!!!”。
std::string::insert()
std::string::insert()
是 C++ 标准库中 std::string
类的成员函数之一,用于在指定位置插入内容到字符串中。
这个函数允许在字符串的特定位置插入另一个字符串、子串或字符。它的重载版本可以用于不同的插入操作,可以指定要插入的位置和内容。
以下是 std::string::insert()
函数的几种重载形式:
1
2
3
4
5
std::string& insert(size_t pos, const std::string& str); // 在指定位置插入另一个字符串
std::string& insert(size_t pos, const std::string& str, size_t subpos, size_t sublen); // 在指定位置插入另一个字符串的一部分
std::string& insert(size_t pos, const char* s); // 在指定位置插入 C 风格的字符串
std::string& insert(size_t pos, const char* s, size_t n); // 在指定位置插入指定长度的 C 风格字符串
std::string& insert(size_t pos, size_t n, char c); // 在指定位置插入重复的字符
以下是一个简单的示例演示了如何使用 std::string::insert()
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
int main() {
std::string str = "Hello world!";
// 在指定位置插入另一个字符串
str.insert(5, "beautiful ");
// 在指定位置插入重复的字符
str.insert(6, 3, '*');
std::cout << "结果字符串: " << str << std::endl;
return 0;
}
在这个示例中,std::string::insert()
函数被用于在字符串 str
的指定位置进行插入操作。首先,在位置 5 插入了另一个字符串 “beautiful “,然后在位置 6 插入了重复的字符 ‘‘。最终输出的字符串将是 “Hello * * beautiful world!”。
std::string::erase()
std::string::erase()
是 C++ 标准库中 std::string
类的成员函数之一,用于从字符串中删除指定位置的字符或一段子串。
这个函数有多个重载版本,允许你指定要删除的位置和要删除的字符数。你可以删除单个字符、一段字符,或者从指定位置开始一直删除到字符串末尾。
以下是 std::string::erase()
函数的几种重载形式:
1
2
3
std::string& erase(size_t pos = 0, size_t len = npos); // 从指定位置开始删除指定数量的字符
iterator erase(iterator position); // 删除指定位置处的字符
iterator erase(iterator first, iterator last); // 删除指定范围内的字符
示例代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, beautiful world!";
// 删除指定位置开始的 7 个字符
str.erase(7, 7); // 从位置 7 开始,删除 7 个字符
// 从迭代器位置开始删除一段字符
std::string::iterator it = str.begin() + 13;
str.erase(it, str.end());
std::cout << "结果字符串: " << str << std::endl;
return 0;
}
在这个示例中,首先使用 erase()
函数从位置 7 开始删除了 7 个字符,然后使用迭代器指定的位置开始删除了一段字符,直到字符串的末尾。最终输出的字符串将是 “Hello, “。
std::string::replace()
std::string::replace()
是 C++ 标准库中 std::string
类的成员函数之一,用于替换字符串中的部分内容。
这个函数允许你用另一个字符串、子串或字符序列替换指定位置和长度的内容。你可以选择性地替换指定位置开始的一定数量的字符,也可以替换整个字符串的一部分。
以下是 std::string::replace()
函数的几种重载形式:
1
2
3
4
5
6
std::string& replace(size_t pos, size_t len, const std::string& str); // 用另一个字符串替换指定位置和长度的内容
std::string& replace(iterator i1, iterator i2, const std::string& str); // 用另一个字符串替换指定范围的内容
std::string& replace(size_t pos, size_t len, const std::string& str, size_t subpos, size_t sublen); // 用另一个字符串的一部分替换指定位置和长度的内容
std::string& replace(size_t pos, size_t len, const char* s); // 用 C 风格的字符串替换指定位置和长度的内容
std::string& replace(iterator i1, iterator i2, const char* s, size_t n); // 用指定长度的 C 风格字符串替换指定范围的内容
std::string& replace(size_t pos, size_t len, size_t n, char c); // 用重复的字符替换指定位置和长度的内容
以下是一个示例演示了如何使用 std::string::replace()
函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, beautiful world!";
// 用另一个字符串替换指定位置和长度的内容
str.replace(7, 9, "wonderful");
// 用重复的字符替换指定位置和长度的内容
str.replace(0, 6, 7, '*');
std::cout << "结果字符串: " << str << std::endl;
return 0;
}
在这个示例中,首先使用 replace()
函数将字符串中位置 7 开始、长度为 9 的部分替换为另一个字符串 “wonderful”,然后用重复的字符 ‘’ 替换了字符串的前 7 个字符。最终输出的字符串将是 “**wonderful world!”。
std::string::substr()
std::string::substr()
是 C++ 标准库中 std::string
类的成员函数之一,用于提取字符串的子串。
这个函数接受两个参数,第一个参数是子串的起始位置(索引),第二个参数是子串的长度。如果只提供一个参数,它将从指定位置开始提取直到字符串末尾的所有字符。
以下是 std::string::substr()
函数的形式:
1
std::string substr(size_t pos, size_t len = npos) const;
pos
是子串的起始位置(索引),从 0 开始计数。len
是子串的长度,默认值是npos
,表示提取从pos
开始到字符串末尾的所有字符。
示例代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
// 提取从索引为 7 开始的子串
std::string sub1 = str.substr(7);
std::cout << "子串1: " << sub1 << std::endl; // 输出 "world!"
// 提取从索引为 0 开始,长度为 5 的子串
std::string sub2 = str.substr(0, 5);
std::cout << "子串2: " << sub2 << std::endl; // 输出 "Hello"
return 0;
}
在这个示例中,str.substr(7)
提取了从索引 7 开始到字符串末尾的子串(”world!”),而 str.substr(0, 5)
提取了从索引 0 开始长度为 5 的子串(”Hello”)。
std::string::swap()
std::string::swap()
是 C++ 标准库中 std::string
类的成员函数之一。它用于交换两个 std::string
对象的内容,但是它的作用并不是直接交换字符串的内容,而是交换它们内部指向字符数据的指针,这样可以在不复制字符串的情况下快速交换它们的内容。
函数原型:
1
void swap(std::string& str);
参数:
str
: 另一个std::string
对象,它的内容将与当前对象的内容进行交换。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello";
std::string str2 = "World";
std::cout << "交换前:\n";
std::cout << "str1: " << str1 << "\n";
std::cout << "str2: " << str2 << "\n";
// 使用 swap() 函数交换 str1 和 str2 的内容
str1.swap(str2);
std::cout << "\n交换后:\n";
std::cout << "str1: " << str1 << "\n";
std::cout << "str2: " << str2 << "\n";
return 0;
}
输出:
1
2
3
4
5
6
7
交换前:
str1: Hello
str2: World
交换后:
str1: World
str2: Hello
str1.swap(str2);
执行后,str1
的内容变为 “World”,str2
的内容变为 “Hello”。这种交换方式使得操作更高效,特别是当处理大量字符数据时。
std::string::find()
std::string::find()
是 C++ 标准库中 std::string
类的成员函数之一,用于在字符串中查找指定子串第一次出现的位置。
函数原型:
1
2
3
4
size_t find(const std::string& str, size_t pos = 0) const noexcept;
size_t find(const char* s, size_t pos = 0) const;
size_t find(const char* s, size_t pos, size_t count) const;
size_t find(char c, size_t pos = 0) const noexcept;
参数:
str
:要查找的子串。s
:要查找的 C 风格字符串(以 null 结尾的字符数组)。count
:要查找的 C 风格字符串中的字符数。c
:要查找的字符。pos
:搜索的起始位置,默认为 0。
返回值:
返回第一次出现子串或字符的位置,如果未找到,则返回 std::string::npos
(static const size_t npos = -1;
)。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
// 查找子串 "World"
size_t found = str.find("World");
if (found != std::string::npos) {
std::cout << "子串 'World' 第一次出现的位置:" << found << std::endl;
} else {
std::cout << "未找到子串 'World'" << std::endl;
}
// 查找字符 'o' 第一次出现的位置
found = str.find('o');
if (found != std::string::npos) {
std::cout << "字符 'o' 第一次出现的位置:" << found << std::endl;
} else {
std::cout << "未找到字符 'o'" << std::endl;
}
return 0;
}
输出:
1
2
子串 'World' 第一次出现的位置:7
字符 'o' 第一次出现的位置:4
这个示例中,str.find("World")
查找子串 “World” 在 str
中第一次出现的位置,返回 7。而 str.find('o')
查找字符 ‘o’ 在 str
中第一次出现的位置,返回 4。
std::string::rfind()
std::string::rfind()
是 C++ 标准库中 std::string
类的成员函数之一,用于在字符串中从末尾开始查找指定子串最后一次出现的位置。
函数原型:
1
2
3
4
size_t rfind(const std::string& str, size_t pos = npos) const noexcept;
size_t rfind(const char* s, size_t pos = npos) const;
size_t rfind(const char* s, size_t pos, size_t count) const;
size_t rfind(char c, size_t pos = npos) const noexcept;
参数:
str
:要查找的子串。s
:要查找的 C 风格字符串(以 null 结尾的字符数组)。count
:要查找的 C 风格字符串中的字符数。c
:要查找的字符。pos
:搜索的起始位置,默认为std::string::npos
,表示从末尾开始搜索。
返回值:
返回最后一次出现子串或字符的位置,如果未找到,则返回 std::string::npos
(static const size_t npos = -1;
)。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World! Hello again.";
// 从末尾开始查找子串 "Hello"
size_t found = str.rfind("Hello");
if (found != std::string::npos) {
std::cout << "子串 'Hello' 最后一次出现的位置:" << found << std::endl;
} else {
std::cout << "未找到子串 'Hello'" << std::endl;
}
// 从末尾开始查找字符 'o'
found = str.rfind('o');
if (found != std::string::npos) {
std::cout << "字符 'o' 最后一次出现的位置:" << found << std::endl;
} else {
std::cout << "未找到字符 'o'" << std::endl;
}
return 0;
}
输出:
1
2
子串 'Hello' 最后一次出现的位置:14
字符 'o' 最后一次出现的位置:24
这个示例中,str.rfind("Hello")
从末尾开始查找子串 “Hello” 在 str
中最后一次出现的位置,返回 14。而 str.rfind('o')
从末尾开始查找字符 ‘o’ 在 str
中最后一次出现的位置,返回 24。
std::string::find_first_of()
std::string::find_first_of()
是 C++ 标准库中 std::string
类的成员函数之一,用于在字符串中查找与给定字符序列中任何字符匹配的第一个位置。
函数原型:
1
2
3
4
size_t find_first_of(const std::string& str, size_t pos = 0) const noexcept;
size_t find_first_of(const char* s, size_t pos = 0) const;
size_t find_first_of(const char* s, size_t pos, size_t count) const;
size_t find_first_of(char c, size_t pos = 0) const noexcept;
参数:
str
:要查找的字符序列。s
:要查找的 C 风格字符串(以 null 结尾的字符数组)。count
:要查找的 C 风格字符串中的字符数。c
:要查找的字符。pos
:搜索的起始位置,默认为 0。
返回值:
返回第一个与字符序列中任何字符匹配的位置,如果未找到,则返回 std::string::npos
(static const size_t npos = -1;
)。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
// 查找第一个匹配字符序列中任何字符的位置
size_t found = str.find_first_of("aeiou");
if (found != std::string::npos) {
std::cout << "第一个匹配元音字母的位置:" << found << std::endl;
} else {
std::cout << "未找到匹配的元音字母" << std::endl;
}
// 查找第一个匹配字符 'o' 的位置
found = str.find_first_of('o');
if (found != std::string::npos) {
std::cout << "第一个匹配字符 'o' 的位置:" << found << std::endl;
} else {
std::cout << "未找到字符 'o'" << std::endl;
}
return 0;
}
输出:
1
2
第一个匹配元音字母的位置:1
第一个匹配字符 'o' 的位置:4
这个示例中,str.find_first_of("aeiou")
查找字符序列中任何元音字母(’a’、’e’、’i’、’o’、’u’)第一次出现的位置,返回 1。而 str.find_first_of('o')
查找字符 ‘o’ 第一次出现的位置,返回 4。
std::string::find_last_of()
std::string::find_last_of()
是 C++ 标准库中 std::string
类的成员函数之一,用于在字符串中查找与给定字符序列中任何字符匹配的最后一个位置。
函数原型:
1
2
3
4
size_t find_last_of(const std::string& str, size_t pos = npos) const noexcept;
size_t find_last_of(const char* s, size_t pos = npos) const;
size_t find_last_of(const char* s, size_t pos, size_t count) const;
size_t find_last_of(char c, size_t pos = npos) const noexcept;
参数:
str
:要查找的字符序列。s
:要查找的 C 风格字符串(以 null 结尾的字符数组)。count
:要查找的 C 风格字符串中的字符数。c
:要查找的字符。pos
:搜索的起始位置,默认为std::string::npos
,表示从末尾开始搜索。
返回值:
返回最后一个与字符序列中任何字符匹配的位置,如果未找到,则返回 std::string::npos
(static const size_t npos = -1;
)。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
// 查找最后一个匹配字符序列中任何字符的位置
size_t found = str.find_last_of("aeiou");
if (found != std::string::npos) {
std::cout << "最后一个匹配元音字母的位置:" << found << std::endl;
} else {
std::cout << "未找到匹配的元音字母" << std::endl;
}
// 查找最后一个匹配字符 'o' 的位置
found = str.find_last_of('o');
if (found != std::string::npos) {
std::cout << "最后一个匹配字符 'o' 的位置:" << found << std::endl;
} else {
std::cout << "未找到字符 'o'" << std::endl;
}
return 0;
}
输出:
1
2
最后一个匹配元音字母的位置:9
最后一个匹配字符 'o' 的位置:8
这个示例中,str.find_last_of("aeiou")
查找字符序列中任何元音字母(’a’、’e’、’i’、’o’、’u’)最后一次出现的位置,返回 9。而 str.find_last_of('o')
查找字符 ‘o’ 最后一次出现的位置,返回 8。
std::string::find_first_not_of()
std::string::find_first_not_of()
是 C++ 标准库中 std::string
类的成员函数之一,用于在字符串中查找第一个不属于给定字符序列的字符位置。
函数原型:
1
2
3
4
size_t find_first_not_of(const std::string& str, size_t pos = 0) const noexcept;
size_t find_first_not_of(const char* s, size_t pos = 0) const;
size_t find_first_not_of(const char* s, size_t pos, size_t count) const;
size_t find_first_not_of(char c, size_t pos = 0) const noexcept;
参数:
str
:要查找的字符序列。s
:要查找的 C 风格字符串(以 null 结尾的字符数组)。count
:要查找的 C 风格字符串中的字符数。c
:要查找的字符。pos
:搜索的起始位置,默认为 0。
返回值:
返回第一个不属于字符序列中任何字符的位置,如果未找到,则返回 std::string::npos
(static const size_t npos = -1;
)。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
int main() {
std::string str = " Hello, World!";
// 查找第一个不属于空格字符的位置
size_t found = str.find_first_not_of(" \t\n\v\f\r");
if (found != std::string::npos) {
std::cout << "第一个不属于空白字符的位置:" << found << std::endl;
} else {
std::cout << "未找到不属于空白字符的位置" << std::endl;
}
// 查找第一个不属于字符 'H' 的位置
found = str.find_first_not_of('H');
if (found != std::string::npos) {
std::cout << "第一个不属于字符 'H' 的位置:" << found << std::endl;
} else {
std::cout << "未找到不属于字符 'H' 的位置" << std::endl;
}
return 0;
}
输出:
1
2
第一个不属于空白字符的位置:3
第一个不属于字符 'H' 的位置:0
这个示例中,str.find_first_not_of(" \t\n\v\f\r")
查找第一个不属于空白字符(空格、制表符、换行符等)的位置,返回 3。而 str.find_first_not_of('H')
查找第一个不属于字符 ‘H’ 的位置,返回 0。
std::string::find_last_not_of()
std::string::find_last_not_of()
是 C++ 标准库中 std::string
类的成员函数之一,用于在字符串中查找最后一个不属于给定字符序列的字符位置。
函数原型:
1
2
3
4
size_t find_last_not_of(const std::string& str, size_t pos = npos) const noexcept;
size_t find_last_not_of(const char* s, size_t pos = npos) const;
size_t find_last_not_of(const char* s, size_t pos, size_t count) const;
size_t find_last_not_of(char c, size_t pos = npos) const noexcept;
参数:
str
:要查找的字符序列。s
:要查找的 C 风格字符串(以 null 结尾的字符数组)。count
:要查找的 C 风格字符串中的字符数。c
:要查找的字符。pos
:搜索的起始位置,默认为std::string::npos
,表示从末尾开始搜索。
返回值:
返回最后一个不属于字符序列中任何字符的位置,如果未找到,则返回 std::string::npos
(static const size_t npos = -1;
)。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
int main() {
std::string str = " Hello, World! ";
// 查找最后一个不属于空格字符的位置
size_t found = str.find_last_not_of(" \t\n\v\f\r");
if (found != std::string::npos) {
std::cout << "最后一个不属于空白字符的位置:" << found << std::endl;
} else {
std::cout << "未找到不属于空白字符的位置" << std::endl;
}
// 查找最后一个不属于字符 '!' 的位置
found = str.find_last_not_of('!');
if (found != std::string::npos) {
std::cout << "最后一个不属于字符 '!' 的位置:" << found << std::endl;
} else {
std::cout << "未找到不属于字符 '!' 的位置" << std::endl;
}
return 0;
}
输出:
1
2
最后一个不属于空白字符的位置:14
最后一个不属于字符 '!' 的位置:13
在这个示例中,str.find_last_not_of(" \t\n\v\f\r")
查找最后一个不属于空白字符(空格、制表符、换行符等)的位置,返回 14。而 str.find_last_not_of('!')
查找最后一个不属于字符 ‘!’ 的位置,返回 13。
std::string::compare()
std::string::compare()
是 C++ 标准库中 std::string
类的成员函数之一,用于比较两个字符串的大小关系。
函数原型:
1
2
3
4
5
6
int compare(const std::string& str) const noexcept;
int compare(size_t pos, size_t len, const std::string& str) const;
int compare(size_t pos, size_t len, const std::string& str, size_t subpos, size_t sublen) const;
int compare(const char* s) const;
int compare(size_t pos, size_t len, const char* s) const;
int compare(size_t pos, size_t len, const char* s, size_t n) const;
参数:
str
:要比较的另一个字符串。s
:要比较的 C 风格字符串(以 null 结尾的字符数组)。pos
:字符串中的起始位置,默认为 0。len
:要比较的字符数,默认为std::string::npos
。subpos
:子串的起始位置,默认为 0。sublen
:子串的字符数,默认为std::string::npos
。n
:要比较的字符数。
返回值:
- 返回值为负数:表示调用对象小于参数字符串。
- 返回值为零:表示调用对象等于参数字符串。
- 返回值为正数:表示调用对象大于参数字符串。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
int main() {
std::string str1 = "apple";
std::string str2 = "banana";
// 比较两个字符串
int result = str1.compare(str2);
if (result < 0) {
std::cout << "str1 小于 str2" << std::endl;
} else if (result == 0) {
std::cout << "str1 等于 str2" << std::endl;
} else {
std::cout << "str1 大于 str2" << std::endl;
}
return 0;
}
输出:
1
str1 小于 str2
在这个示例中,str1.compare(str2)
比较了两个字符串 str1
和 str2
。由于 “apple” 在字典序中小于 “banana”,所以返回值为负数,输出表明 str1
小于 str2
。
std::stoi()
std::stoi()
是 C++ 标准库中 <string>
头文件中提供的函数之一,用于将字符串转换为整数类型(int
)。
函数原型:
1
int stoi(const std::string& str, size_t* pos = 0, int base = 10);
参数:
str
:要转换为整数的字符串。pos
:指向存储第一个无效字符位置的指针,可选参数,默认为nullptr
。base
:进制,默认为 10(十进制)。可以是 0 或者介于 2 到 36 之间的值,表示字符序列的进制基数。
返回值:
- 返回转换后的整数值。如果转换过程中出现错误,会抛出
std::invalid_argument
或std::out_of_range
异常。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
int main() {
std::string str = "12345";
try {
int num = std::stoi(str);
std::cout << "转换后的整数为: " << num << std::endl;
} catch (const std::exception& e) {
std::cout << "转换失败: " << e.what() << std::endl;
}
return 0;
}
输出:
1
转换后的整数为: 12345
在这个示例中,std::stoi()
将字符串 “12345” 转换为整数类型并输出。如果无法转换字符串为整数,例如,当字符串包含非数字字符时,将抛出异常。
std::stol()
std::stol()
是 C++ 标准库中 <string>
头文件中提供的函数之一,用于将字符串转换为长整型(long
)。
函数原型:
1
long stol(const std::string& str, size_t* pos = 0, int base = 10);
参数:
str
:要转换为长整型的字符串。pos
:指向存储第一个无效字符位置的指针,可选参数,默认为nullptr
。base
:进制,默认为 10(十进制)。可以是 0 或者介于 2 到 36 之间的值,表示字符序列的进制基数。
返回值:
- 返回转换后的长整型值。如果转换过程中出现错误,会抛出
std::invalid_argument
或std::out_of_range
异常。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
int main() {
std::string str = "123456789";
try {
long num = std::stol(str);
std::cout << "转换后的长整数为: " << num << std::endl;
} catch (const std::exception& e) {
std::cout << "转换失败: " << e.what() << std::endl;
}
return 0;
}
输出:
1
转换后的长整数为: 123456789
这个示例中,std::stol()
将字符串 “123456789” 转换为长整数类型并输出。如果无法转换字符串为长整数,例如,当字符串包含非数字字符时,将抛出异常。
std::stoll()
std::stoll()
是 C++ 标准库中 <string>
头文件中提供的函数之一,用于将字符串转换为长长整型(long long
)。
函数原型:
1
long long stoll(const std::string& str, size_t* pos = 0, int base = 10);
参数:
str
:要转换为长长整型的字符串。pos
:指向存储第一个无效字符位置的指针,可选参数,默认为nullptr
。base
:进制,默认为 10(十进制)。可以是 0 或者介于 2 到 36 之间的值,表示字符序列的进制基数。
返回值:
- 返回转换后的长长整型值。如果转换过程中出现错误,会抛出
std::invalid_argument
或std::out_of_range
异常。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
int main() {
std::string str = "1234567890123456789";
try {
long long num = std::stoll(str);
std::cout << "转换后的长长整数为: " << num << std::endl;
} catch (const std::exception& e) {
std::cout << "转换失败: " << e.what() << std::endl;
}
return 0;
}
输出:
1
转换后的长长整数为: 1234567890123456789
这个示例中,std::stoll()
将字符串 “1234567890123456789” 转换为长长整数类型并输出。如果无法转换字符串为长长整数,例如,当字符串包含非数字字符时,将抛出异常。
std::stof()
std::stof()
是 C++ 标准库中 <string>
头文件中提供的函数之一,用于将字符串转换为单精度浮点数(float
)。
函数原型:
1
float stof(const std::string& str, size_t* pos = 0);
参数:
str
:要转换为单精度浮点数的字符串。pos
:指向存储第一个无效字符位置的指针,可选参数,默认为nullptr
。
返回值:
- 返回转换后的单精度浮点数值。如果转换过程中出现错误,会抛出
std::invalid_argument
或std::out_of_range
异常。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
int main() {
std::string str = "3.14159";
try {
float num = std::stof(str);
std::cout << "转换后的单精度浮点数为: " << num << std::endl;
} catch (const std::exception& e) {
std::cout << "转换失败: " << e.what() << std::endl;
}
return 0;
}
输出:
1
转换后的单精度浮点数为: 3.14159
这个示例中,std::stof()
将字符串 “3.14159” 转换为单精度浮点数类型并输出。如果无法转换字符串为单精度浮点数,例如,当字符串包含非数字字符时,将抛出异常。
std::stod()
std::stod()
是 C++ 标准库中 <string>
头文件中提供的函数之一,用于将字符串转换为双精度浮点数(double
)。
函数原型:
1
double stod(const std::string& str, size_t* pos = 0);
参数:
str
:要转换为双精度浮点数的字符串。pos
:指向存储第一个无效字符位置的指针,可选参数,默认为nullptr
。
返回值:
- 返回转换后的双精度浮点数值。如果转换过程中出现错误,会抛出
std::invalid_argument
或std::out_of_range
异常。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
int main() {
std::string str = "3.14159265359";
try {
double num = std::stod(str);
std::cout << "转换后的双精度浮点数为: " << num << std::endl;
} catch (const std::exception& e) {
std::cout << "转换失败: " << e.what() << std::endl;
}
return 0;
}
输出:
1
转换后的双精度浮点数为: 3.14159265359
这个示例中,std::stod()
将字符串 “3.14159265359” 转换为双精度浮点数类型并输出。如果无法转换字符串为双精度浮点数,例如,当字符串包含非数字字符时,将抛出异常。
std::stold()
std::stold()
是 C++ 标准库中 <string>
头文件中提供的函数之一,用于将字符串转换为长双精度浮点数(long double
)。
函数原型:
1
long double stold(const std::string& str, size_t* pos = 0);
参数:
str
:要转换为长双精度浮点数的字符串。pos
:指向存储第一个无效字符位置的指针,可选参数,默认为nullptr
。
返回值:
- 返回转换后的长双精度浮点数值。如果转换过程中出现错误,会抛出
std::invalid_argument
或std::out_of_range
异常。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
int main() {
std::string str = "3.141592653589793238";
try {
long double num = std::stold(str);
std::cout << "转换后的长双精度浮点数为: " << num << std::endl;
} catch (const std::exception& e) {
std::cout << "转换失败: " << e.what() << std::endl;
}
return 0;
}
输出:
1
转换后的长双精度浮点数为: 3.141592653589793238
这个示例中,std::stold()
将字符串 “3.141592653589793238” 转换为长双精度浮点数类型并输出。如果无法转换字符串为长双精度浮点数,例如,当字符串包含非数字字符时,将抛出异常。
std::string::c_str()
std::string::c_str()
是 C++ 标准库中 std::string
类的成员函数,用于返回一个指向以 null 结尾的 C 风格字符串(字符数组)的指针,即指向 std::string
对象中存储的字符数据。
函数原型:
1
const char* c_str() const noexcept;
返回值:
返回一个指向以 null 结尾的字符数组的指针,该字符数组存储了 std::string
对象中的字符串内容。
示例:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
const char* cstr = str.c_str();
std::cout << "C 风格字符串为: " << cstr << std::endl;
return 0;
}
输出:
1
C 风格字符串为: Hello, World!
在这个示例中,str.c_str()
返回一个指向字符串 “Hello, World!” 的 C 风格字符串的指针,并通过 std::cout
输出该字符串。需要注意的是,c_str()
返回的指针指向的字符数组是以 null 结尾的,因此可以被正常地当做 C 风格字符串处理。
std::string::data()
std::string::data()
是 C++ 标准库中 std::string
类的成员函数,用于返回一个指向字符数据的指针,但不一定以 null 结尾。
函数原型:
1
const char* data() const noexcept;
返回值:
返回一个指向存储在 std::string
对象中的字符数据的指针。与 c_str()
不同的是,data()
不一定会在字符串的末尾添加 null 终止符。
示例:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
const char* strData = str.data();
std::cout << "字符数据为: " << strData << std::endl;
return 0;
}
输出:
1
字符数据为: Hello, World!
在这个示例中,str.data()
返回一个指向字符串 “Hello, World!” 的字符数据的指针,并通过 std::cout
输出该字符串。需要注意的是,data()
返回的指针可能不以 null 结尾,并且对返回指针所指向的数据进行修改时,需要谨慎处理末尾的 null 终止符。
std::string::reserve()
std::string::reserve()
是 C++ 标准库中 std::string
类的成员函数之一,用于请求字符串预留足够的内存空间,以容纳指定数量的字符,而不会更改字符串的大小。
函数原型:
1
void reserve(size_t n);
参数:
n
:要预留的字符数量。
返回值:
- 无(
void
)。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
int main() {
std::string str = "Hello";
std::cout << "初始容量: " << str.capacity() << std::endl;
str.reserve(20); // 预留至少可以容纳 20 个字符的空间
std::cout << "预留后的容量: " << str.capacity() << std::endl;
return 0;
}
输出:
1
2
初始容量: 5
预留后的容量: 20
在这个示例中,str.reserve(20)
调用了 reserve()
函数来预留至少可以容纳 20 个字符的空间。请注意,reserve()
只是预留了空间,并不改变字符串的长度。这对于在预先知道要存储大量字符的情况下,避免多次重新分配内存而提高性能是很有用的。
std::string::shrink_to_fit()
std::string::shrink_to_fit()
是 C++ 标准库中 std::string
类的成员函数之一,用于要求字符串收缩其容量以适应当前字符串的大小。
函数原型:
1
void shrink_to_fit();
返回值:
- 无(
void
)。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
int main() {
std::string str = "Hello";
std::cout << "初始容量: " << str.capacity() << std::endl;
str.reserve(100); // 分配较大的空间
std::cout << "扩大容量后的容量: " << str.capacity() << std::endl;
str.shrink_to_fit(); // 收缩容量以适应当前大小
std::cout << "收缩容量后的容量: " << str.capacity() << std::endl;
return 0;
}
输出:
1
2
3
初始容量: 5
扩大容量后的容量: 100
收缩容量后的容量: 5
在这个示例中,首先创建了一个字符串 str
,然后通过 str.reserve(100)
手动扩大了其容量。随后调用 str.shrink_to_fit()
来收缩字符串的容量以适应当前字符串的大小。这对于释放不再需要的内存空间是有用的,可以减少不必要的内存浪费。
std::string::capacity()
std::string::capacity()
是 C++ 标准库中 std::string
类的成员函数之一,用于返回当前字符串能够容纳的字符数量,即分配给字符串的内存空间大小。
函数原型:
1
size_t capacity() const noexcept;
返回值:
返回字符串当前可容纳的字符数量,即字符串所分配的内存空间大小。
示例:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
int main() {
std::string str = "Hello";
std::cout << "字符串当前容量: " << str.capacity() << std::endl;
return 0;
}
输出:
1
字符串当前容量: 15
在这个示例中,str.capacity()
返回了字符串 str
当前的容量。需要注意的是,字符串的容量并不等同于字符串的长度(即字符个数),而是指字符串当前已分配的内存大小。容量通常大于或等于字符串的长度,因为字符串预留了额外的空间以便于扩展和操作。
std::getline()
std::getline()
是 C++ 标准库中 <string>
头文件中提供的函数,用于从输入流中读取一行文本并存储到字符串中。
函数原型:
1
std::istream& getline(std::istream& is, std::string& str, char delim = '\n');
参数:
is
:输入流,例如std::cin
(标准输入)或文件流。str
:用于存储输入行的字符串。delim
:可选参数,表示行的结束符,默认为换行符'\n'
。
返回值:
- 返回输入流
is
的引用,以便进行链式输入。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "请输入一行文本:" << std::endl;
std::getline(std::cin, input);
std::cout << "你输入的内容是:" << input << std::endl;
return 0;
}
输出:
1
2
3
请输入一行文本:
[用户输入]
你输入的内容是:[用户输入的内容]
在示例中,std::getline(std::cin, input)
从标准输入中读取一行用户输入的文本,并将其存储在 input
字符串中。如果输入的行超过了字符串的最大长度,std::getline()
会自动调整字符串的大小以容纳整个输入行。
std::to_string()
std::to_string()
是 C++ 标准库中 <string>
头文件中提供的函数之一,用于将数值类型转换为对应的字符串形式。
函数原型:
1
2
3
4
5
6
7
8
9
std::string to_string(int value);
std::string to_string(long value);
std::string to_string(long long value);
std::string to_string(unsigned value);
std::string to_string(unsigned long value);
std::string to_string(unsigned long long value);
std::string to_string(float value);
std::string to_string(double value);
std::string to_string(long double value);
参数:
- 不同版本的
to_string()
接受不同的数值类型作为参数,并将其转换为对应的字符串形式。
返回值:
- 返回包含数值转换后的字符串。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
int main() {
int intValue = 42;
float floatValue = 3.14159f;
std::string strInt = std::to_string(intValue);
std::string strFloat = std::to_string(floatValue);
std::cout << "整数转换后的字符串: " << strInt << std::endl;
std::cout << "浮点数转换后的字符串: " << strFloat << std::endl;
return 0;
}
输出:
1
2
整数转换后的字符串: 42
浮点数转换后的字符串: 3.141590
在这个示例中,std::to_string()
将整数和浮点数分别转换为对应的字符串形式,并将其存储在 strInt
和 strFloat
中。这是一个方便的函数,用于将不同类型的数值转换为字符串,以便于输出或字符串处理。