命名空间
变体
操作

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) (C++17 起)

类模板 basic_string 存储和操作字符类对象序列,这些对象是非数组的 字符类型,为 TrivialTypeStandardLayoutType。此类型不依赖于字符类型,也不依赖于该类型上的操作性质。操作的定义由 `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 不用于元素的构造/析构),SequenceContainerContiguousContainer(C++17 起) 的要求。

如果 `Traits::char_type` 和 `Allocator::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 - 字符类型
特性 - 指定字符类型操作的 trait 类
Allocator - 用于分配内部存储的 Allocator 类型

[编辑] 嵌套类型

类型 定义
traits_type 特性
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 前)

LegacyRandomAccessIteratorcontiguous_iteratorConstexprIteratorvalue_type

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

LegacyRandomAccessIteratorLegacyContiguousIteratorconst value_type

(C++20 前)

LegacyRandomAccessIteratorcontiguous_iteratorConstexprIteratorconst value_type

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

[编辑] 数据成员

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

[编辑] 成员函数

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

[编辑] 非成员函数

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

[编辑] 字面量

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

[编辑] 辅助类

字符串的哈希支持
(类模板特化) [编辑]

[编辑] 推导指南 (C++17 起)

[编辑] 迭代器失效

引用、指针和迭代器指向 basic_string 元素的有效性可能会因任何接受非 const basic_string 引用作为参数的标准库函数而失效,例如 std::getlinestd::swapoperator>>,以及调用非 const 成员函数,除了 operator[]atdatafrontbackbeginrbeginendrend

[编辑]

尽管在 C++23 之前,构造或销毁 `std::basic_string` 的元素时需要使用自定义的 `construct` 或 `destroy`,但所有实现都只使用了默认机制。此要求已通过 P1072R10 纠正,以符合现有实践。

特性测试 标准 特性
__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) 接受容器兼容范围的构造、插入和替换成员函数

[编辑] 示例

#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

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
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 中修复。

[编辑] 另请参阅

只读字符串视图
(类模板) [编辑]

[编辑] 外部链接

C++ 字符串处理