命名空间
变体
操作

std::basic_string

来自 cppreference.cn
< cpp‎ | string
 
 
 
std::basic_string
 
定义于头文件 <string>
template<

    class CharT,
    class Traits = std::char_traits<CharT>,
    class Allocator = std::allocator<CharT>

> class basic_string;
(1)
namespace pmr {

template<
    class CharT,
    class Traits = std::char_traits<CharT>
> using basic_string =
    std::basic_string<CharT, Traits, std::pmr::polymorphic_allocator<CharT>>;

}
(2) (since C++17)

类模板 basic_string 存储和操作类字符对象的序列,这些对象是 TrivialTypeStandardLayoutType 的非数组对象。该类既不依赖于字符类型,也不依赖于对该类型进行的操作的性质。操作的定义通过 Traits 模板参数提供 - std::char_traits 的特化或兼容的 traits 类。

basic_string 的元素是连续存储的,也就是说,对于 basic_string s,对于任何在 [0s.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 不用于元素的构造/析构),SequenceContainerContiguousContainer(自 C++17 起) 的要求。

如果 Traits::char_typeAllocator::char_type 中的任何一个与 CharT 不同,则程序是非良构的。

std::basic_string 的所有成员函数都是 constexpr:可以在常量表达式的求值中创建和使用 std::basic_string 对象。

但是,std::basic_string 对象通常不能是 constexpr,因为任何动态分配的存储都必须在相同的常量表达式求值中释放。

(自 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
Allocator::size_type (直到 C++11)
std::allocator_traits<Allocator>::size_type (自 C++11 起)
[编辑]
difference_type
Allocator::difference_type (直到 C++11)
std::allocator_traits<Allocator>::difference_type (自 C++11 起)
[编辑]
reference value_type&[编辑]
const_reference const value_type&[编辑]
pointer

Allocator::pointer

(直到 C++11)

std::allocator_traits<Allocator>::pointer

(自 C++11 起)
[编辑]
const_pointer

Allocator::const_pointer

(直到 C++11)

std::allocator_traits<Allocator>::const_pointer

(自 C++11 起)
[编辑]
iterator

LegacyRandomAccessIteratorLegacyContiguousIteratorvalue_type

(直到 C++20)

LegacyRandomAccessIterator, contiguous_iterator, 和 ConstexprIteratorvalue_type

(自 C++20 起)
[编辑]
const_iterator

LegacyRandomAccessIteratorLegacyContiguousIteratorconst value_type

(直到 C++20)

LegacyRandomAccessIterator, contiguous_iterator, 和 ConstexprIteratorconst value_type

(自 C++20 起)
[编辑]
reverse_iterator std::reverse_iterator<iterator>[编辑]
const_reverse_iterator std::reverse_iterator<const_iterator>[编辑]

[编辑] 数据成员

constexpr size_type npos [静态] 特殊值 size_type(-1),其确切含义取决于上下文

[编辑] 成员函数

构造一个 basic_string
(公共成员函数) [编辑]
销毁字符串,如果使用则释放内部存储
(公共成员函数) [编辑]
为字符串赋值
(公共成员函数) [编辑]
为字符串赋值字符
(公共成员函数) [编辑]
为字符串赋值字符范围
(公共成员函数) [编辑]
返回关联的分配器
(公共成员函数) [编辑]
元素访问
访问指定字符,带边界检查
(公共成员函数) [编辑]
访问指定字符
(公共成员函数) [编辑]
(DR*)
访问第一个字符
(公共成员函数) [编辑]
(DR*)
访问最后一个字符
(公共成员函数) [编辑]
返回指向字符串第一个字符的指针
(公共成员函数) [编辑]
返回字符串的非模​​改标准 C 字符数组版本
(公共成员函数) [编辑]
返回指向整个字符串的非模​​改 basic_string_view
(公共成员函数) [编辑]
迭代器
返回指向开始的迭代器
(公共成员函数) [编辑]
(C++11)
返回指向结尾的迭代器
(公共成员函数) [编辑]
返回指向开始的反向迭代器
(公共成员函数) [编辑]
(C++11)
返回指向结尾的反向迭代器
(公共成员函数) [编辑]
容量
检查字符串是否为空
(公共成员函数) [编辑]
返回字符数
(公共成员函数) [编辑]
返回最大字符数
(公共成员函数) [编辑]
预留存储空间
(公共成员函数) [编辑]
返回当前已分配存储空间中可以容纳的字符数
(公共成员函数) [编辑]
通过释放未使用的内存来减少内存使用
(公共成员函数) [编辑]
修改器
清除内容
(公共成员函数) [编辑]
插入字符
(公共成员函数) [编辑]
插入字符范围
(公共成员函数) [编辑]
移除字符
(公共成员函数) [编辑]
在末尾追加一个字符
(公共成员函数) [编辑]
移除最后一个字符
(公共成员函数) [编辑]
在末尾追加字符
(公共成员函数) [编辑]
在末尾追加字符范围
(公共成员函数) [编辑]
在末尾追加字符
(公共成员函数) [编辑]
替换字符串的指定部分
(公共成员函数) [编辑]
用字符范围替换字符串的指定部分
(公共成员函数) [编辑]
复制字符
(公共成员函数) [编辑]
更改存储的字符数
(公共成员函数) [编辑]
更改存储的字符数,并可能通过用户提供的操作覆盖不确定的内容
(公共成员函数) [编辑]
交换内容
(公共成员函数) [编辑]
搜索
查找给定子字符串的第一次出现
(公共成员函数) [编辑]
查找子字符串的最后一次出现
(公共成员函数) [编辑]
查找字符的第一次出现
(公共成员函数) [编辑]
查找字符第一次缺失
(公共成员函数) [编辑]
查找字符的最后一次出现
(公共成员函数) [编辑]
查找字符最后一次缺失
(公共成员函数) [编辑]
操作
比较两个字符串
(公共成员函数) [编辑]
检查字符串是否以给定前缀开头
(公共成员函数) [编辑]
(C++20)
检查字符串是否以给定后缀结尾
(公共成员函数) [编辑]
(C++23)
检查字符串是否包含给定的子字符串或字符
(public member function) [edit]
返回子字符串
(public member function) [edit]

[edit] 非成员函数

连接两个字符串,一个字符串和一个 char,或一个字符串和一个 string_view
(function template) [edit]
(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20)
按字典顺序比较两个字符串
(function template) [edit]
特化 std::swap 算法
(function template) [edit]
移除所有满足特定标准的元素
(function template) [edit]
输入/输出
对字符串执行流输入和输出
(function template) [edit]
从 I/O 流中读取数据到字符串
(function template) [edit]
数值转换
(C++11)(C++11)(C++11)
将字符串转换为有符号整数
(function) [edit]
(C++11)(C++11)
将字符串转换为无符号整数
(function) [edit]
(C++11)(C++11)(C++11)
将字符串转换为浮点值
(function) [edit]
(C++11)
将整型或浮点型值转换为 string
(function) [edit]
将整型或浮点型值转换为 wstring
(function) [edit]

[edit] 字面量

定义在内联命名空间 std::literals::string_literals
将字符数组字面量转换为 basic_string
(function) [edit]

[edit] 辅助类

字符串的哈希支持
(class template specialization) [edit]

[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 的元素时,要求使用自定义的 constructdestroy,但所有实现都只使用了默认机制。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_typeTraits::char_type 更改为 CharT
LWG 2994
(P1148R0)
C++98 如果 Traits::char_type[1] 中的任何一个
Allocator::char_typeCharT 不同,则行为未定义
在这种情况下,程序是
非良构的
  1. Traits::char_type 的情况已在 P1148R0 中修复。

[edit] 参见

只读字符串视图
(class template) [edit]

[edit] 外部链接

C++ 字符串处理