std::basic_string
定义于头文件 <string> |
||
template< class CharT, |
(1) | |
namespace pmr { template< |
(2) | (since C++17) |
类模板 basic_string
存储和操作类字符对象的序列,这些对象是 TrivialType 和 StandardLayoutType 的非数组对象。该类既不依赖于字符类型,也不依赖于对该类型进行的操作的性质。操作的定义通过 Traits
模板参数提供 - std::char_traits 的特化或兼容的 traits 类。
basic_string
的元素是连续存储的,也就是说,对于 basic_string
s,对于任何在 [
0,
s.size())
范围内的 n,&*(s.begin() + n) == &*s.begin() + n ,并且 *(s.begin() + s.size()) 的值为 CharT()(空终止符)(自 C++11 起);或者,等效地,指向 s[0] 的指针可以传递给期望指向 CharT
的 数组(直到 C++11)空终止数组(自 C++11 起) 的第一个元素的函数。
std::basic_string
满足 AllocatorAwareContainer (除了定制的 construct
/destroy
不用于元素的构造/析构),SequenceContainer 和 ContiguousContainer(自 C++17 起) 的要求。
如果 Traits::char_type
和 Allocator::char_type
中的任何一个与 CharT
不同,则程序是非良构的。
|
(自 C++20 起) |
为常见的字符类型提供了几个 typedef
定义于头文件
<string> | |
类型 | 定义 |
std::string
|
std::basic_string<char> |
std::wstring
|
std::basic_string<wchar_t> |
std::u8string (C++20) |
std::basic_string<char8_t> |
std::u16string (C++11) |
std::basic_string<char16_t> |
std::u32string (C++11) |
std::basic_string<char32_t> |
std::pmr::string (C++17) |
std::pmr::basic_string<char> |
std::pmr::wstring (C++17) |
std::pmr::basic_string<wchar_t> |
std::pmr::u8string (C++20) |
std::pmr::basic_string<char8_t> |
std::pmr::u16string (C++17) |
std::pmr::basic_string<char16_t> |
std::pmr::u32string (C++17) |
std::pmr::basic_string<char32_t> |
内容 |
[编辑] 模板参数
CharT | - | 字符类型 |
Traits | - | traits 类,指定字符类型上的操作 |
Allocator | - | Allocator 类型,用于分配内部存储 |
[编辑] 嵌套类型
类型 | 定义 | ||||
traits_type
|
Traits
| ||||
value_type
|
CharT
| ||||
allocator_type
|
Allocator | ||||
size_type
|
| ||||
difference_type
|
| ||||
reference
|
value_type& | ||||
const_reference
|
const value_type& | ||||
pointer
|
| ||||
const_pointer
|
| ||||
iterator
|
| ||||
const_iterator
|
| ||||
reverse_iterator
|
std::reverse_iterator<iterator> | ||||
const_reverse_iterator
|
std::reverse_iterator<const_iterator> |
[编辑] 数据成员
constexpr size_type npos [静态] |
特殊值 size_type(-1),其确切含义取决于上下文 |
[编辑] 成员函数
构造一个 basic_string (公共成员函数) | |
销毁字符串,如果使用则释放内部存储 (公共成员函数) | |
为字符串赋值 (公共成员函数) | |
为字符串赋值字符 (公共成员函数) | |
(C++23) |
为字符串赋值字符范围 (公共成员函数) |
返回关联的分配器 (公共成员函数) | |
元素访问 | |
访问指定字符,带边界检查 (公共成员函数) | |
访问指定字符 (公共成员函数) | |
(DR*) |
访问第一个字符 (公共成员函数) |
(DR*) |
访问最后一个字符 (公共成员函数) |
返回指向字符串第一个字符的指针 (公共成员函数) | |
返回字符串的非模改标准 C 字符数组版本 (公共成员函数) | |
(C++17) |
返回指向整个字符串的非模改 basic_string_view (公共成员函数) |
迭代器 | |
(C++11) |
返回指向开始的迭代器 (公共成员函数) |
(C++11) |
返回指向结尾的迭代器 (公共成员函数) |
(C++11) |
返回指向开始的反向迭代器 (公共成员函数) |
(C++11) |
返回指向结尾的反向迭代器 (公共成员函数) |
容量 | |
检查字符串是否为空 (公共成员函数) | |
返回字符数 (公共成员函数) | |
返回最大字符数 (公共成员函数) | |
预留存储空间 (公共成员函数) | |
返回当前已分配存储空间中可以容纳的字符数 (公共成员函数) | |
(DR*) |
通过释放未使用的内存来减少内存使用 (公共成员函数) |
修改器 | |
清除内容 (公共成员函数) | |
插入字符 (公共成员函数) | |
(C++23) |
插入字符范围 (公共成员函数) |
移除字符 (公共成员函数) | |
在末尾追加一个字符 (公共成员函数) | |
(DR*) |
移除最后一个字符 (公共成员函数) |
在末尾追加字符 (公共成员函数) | |
(C++23) |
在末尾追加字符范围 (公共成员函数) |
在末尾追加字符 (公共成员函数) | |
替换字符串的指定部分 (公共成员函数) | |
(C++23) |
用字符范围替换字符串的指定部分 (公共成员函数) |
复制字符 (公共成员函数) | |
更改存储的字符数 (公共成员函数) | |
(C++23) |
更改存储的字符数,并可能通过用户提供的操作覆盖不确定的内容 (公共成员函数) |
交换内容 (公共成员函数) | |
搜索 | |
查找给定子字符串的第一次出现 (公共成员函数) | |
查找子字符串的最后一次出现 (公共成员函数) | |
查找字符的第一次出现 (公共成员函数) | |
查找字符第一次缺失 (公共成员函数) | |
查找字符的最后一次出现 (公共成员函数) | |
查找字符最后一次缺失 (公共成员函数) | |
操作 | |
比较两个字符串 (公共成员函数) | |
(C++20) |
检查字符串是否以给定前缀开头 (公共成员函数) |
(C++20) |
检查字符串是否以给定后缀结尾 (公共成员函数) |
(C++23) |
检查字符串是否包含给定的子字符串或字符 (public member function) |
返回子字符串 (public member function) |
[edit] 非成员函数
连接两个字符串,一个字符串和一个 char,或一个字符串和一个 string_view (function template) | |
(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20) |
按字典顺序比较两个字符串 (function template) |
特化 std::swap 算法 (function template) | |
移除所有满足特定标准的元素 (function template) | |
输入/输出 | |
对字符串执行流输入和输出 (function template) | |
从 I/O 流中读取数据到字符串 (function template) | |
数值转换 | |
(C++11)(C++11)(C++11) |
将字符串转换为有符号整数 (function) |
(C++11)(C++11) |
将字符串转换为无符号整数 (function) |
(C++11)(C++11)(C++11) |
将字符串转换为浮点值 (function) |
(C++11) |
将整型或浮点型值转换为 string (function) |
(C++11) |
将整型或浮点型值转换为 wstring (function) |
[edit] 字面量
定义在内联命名空间
std::literals::string_literals 中 | |
(C++14) |
将字符数组字面量转换为 basic_string (function) |
[edit] 辅助类
(C++11) |
字符串的哈希支持 (class template specialization) |
[edit] 推导指引 (since C++17)
[edit] 迭代器失效
引用、指针和迭代器,如果它们指向 basic_string
的元素,则可能因任何接受非常量 basic_string
引用的标准库函数而失效,例如 std::getline, std::swap, 或 operator>>,以及通过调用非常量成员函数(除了 operator[], at
, data
, front
, back
, begin
, rbegin
, end
, 和 rend
之外)。
[edit] 注意
尽管在 C++23 之前,构造或销毁 std::basic_string
的元素时,要求使用自定义的 construct
或 destroy
,但所有实现都只使用了默认机制。P1072R10 修正了此要求,以符合现有实践。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_string_udls |
201304L |
(C++14) | 字符串类型的用户定义字面量 |
__cpp_lib_starts_ends_with |
201711L |
(C++20) | starts_with , ends_with |
__cpp_lib_constexpr_string |
201907L |
(C++20) | std::basic_string 的 constexpr |
__cpp_lib_char8_t |
201907L |
(C++20) | std::u8string
|
__cpp_lib_erase_if |
202002L |
(C++20) | erase , erase_if |
__cpp_lib_string_contains |
202011L |
(C++23) | contains
|
__cpp_lib_string_resize_and_overwrite |
202110L |
(C++23) | resize_and_overwrite
|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 接受 容器兼容范围 的构造、插入和替换的成员函数 |
[edit] 示例
#include <iostream> #include <string> int main() { using namespace std::literals; // Creating a string from const char* std::string str1 = "hello"; // Creating a string using string literal auto str2 = "world"s; // Concatenating strings std::string str3 = str1 + " " + str2; // Print out the result std::cout << str3 << '\n'; std::string::size_type pos = str3.find(" "); str1 = str3.substr(pos + 1); // the part after the space str2 = str3.substr(0, pos); // the part till the space std::cout << str1 << ' ' << str2 << '\n'; // Accessing an element using subscript operator[] std::cout << str1[0] << '\n'; str1[0] = 'W'; std::cout << str1 << '\n'; }
输出
hello world world hello w World
[edit] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 530 | C++98 | basic_string 元素存储的连续性由于 LWG259 的失误,意外地变为非必需 |
再次变为必需 |
LWG 2861 | C++98 | value_type 是 Traits::char_type |
更改为 CharT |
LWG 2994 (P1148R0) |
C++98 | 如果 Traits::char_type [1] 中的任何一个和 Allocator::char_type 与 CharT 不同,则行为未定义 |
在这种情况下,程序是 非良构的 |
[edit] 参见
(C++17) |
只读字符串视图 (class template) |
[edit] 外部链接
C++ 字符串处理 |