请注意,本文尚未完成!您可以通过编辑此文档页面来提供帮助。
概述
- 简化
- 详细
template< class CharT, /* ... */ >
class basic_string;
- 常规
- 多态版 (C++17 起)
template<
class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
>
class basic_string;
namespace pmr {
template< class CharT, class Traits = std::char_traits<CharT> >
using basic_string = std::basic_string<CharT, Traits,
std::polymorphic_allocator<CharT>>;
}
一个用于存储和操作字符序列的类。
内存
字符串的元素在内存中连续存储。这意味着指向 `string` 元素的指针可以传递给任何需要指向字符数组元素的指针的函数。

别名
类型 `std::string` 实际上是以下类型的别名:`std::basic_string
可用的 `std::basic_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> |
技术细节
字符串的技术定义
类模板 `basic_string` 存储和操作 `char` 样对象的序列,这些对象是平凡的 标准布局类型的非数组对象。该类既不依赖于字符类型,也不依赖于该类型上的操作的性质。
操作的定义通过 `Traits` 模板参数提供——它是 `std::char_traits` 的特化或兼容的 traits 类。`Traits::char_type` 和 `CharT` 必须命名相同的类型;否则程序格式不正确。
.`basic_string` 的元素是连续存储的,即对于 `basic_string` s,对于 **[ 0, s.size() )** 中的任何 **n**,`&*(s.begin() + n) == &*s.begin() + n`,或者等效地,指向 `s[0]` 的指针可以传递给期望指向空终止 (C++11 起) `CharT[]` 数组的第一个元素的函数的指针。
std::basic_string
定义于 | 字符串 |
模板参数
公共 | CharT | 一个字符类型。 |
公共 | 特性 | 指定字符类型操作的特性类。 |
公共 | 分配器 | 用于分配内部存储的分配器类型。 |
类型名称
公共 | traits_type | 特性 |
公共 | value_type | CharT |
公共 | allocator_type | 分配器 |
公共 | 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 | value_type const& |
公共 | 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 起) |
公共 | 迭代器 | `LegacyRandomAccessIterator` 和 `LegacyContiguousIterator` 到 `value_type` (自 C++20 起)`LegacyRandomAccessIterator` 和 `LegacyConstexprIterator` 和 `contiguous_iterator` 到 `value_type` (自 C++20 起) |
公共 | reverse_iterator | std::reverse_iterator<iterator> |
公共 | const_reverse_iterator | std::reverse_iterator<const_iterator> |
成员函数
公共 | (构造函数) | 构造一个 `basic_string`。 |
公共 | (析构函数) | 销毁字符串,如果使用了内部存储,则解除分配。 |
公共 | operator= | 给字符串赋值。 |
公共 | assign | 给字符串赋字符。 |
公共 | get_allocator | 返回关联的分配器。 |
元素访问
公共 | at | 进行边界检查以访问指定字符。 |
公共 | operator[] | 访问指定字符。 |
公共 | front (自 C++11 起) | 返回第一个字符。 |
公共 | 末尾 (自 C++11 起) | 返回最后一个字符。 |
公共 | 数据 | 返回指向字符串第一个字符的指针。 |
公共 | c_str | 返回字符串的不可修改的标准 C 字符数组版本。 |
公共 | operator basic_string_view (自 C++17 起) | 返回整个字符串的不可修改的 `string_view`。 |
迭代器
公共 | begin cbegin (自 C++11 起) | 返回指向起始元素的 |
公共 | end cend (自 C++11 起) | 返回指向结束元素的 |
公共 | rbegin crbegin (自 C++11 起) | 返回一个反向 `iterator/const_iterator` 到开头。 |
公共 | rend crend (自 C++11 起) | 返回一个反向 `iterator/const_iterator` 到末尾。 |
容量
公共 | empty | 如果字符串为空则返回 `true`,否则返回 `false`。 |
公共 | size length | 返回字符数。 |
公共 | max_size | 返回最大字符数。 |
公共 | reserve | 预留存储空间 |
公共 | capacity | 返回当前已分配存储中可容纳的字符数 |
公共 | shrink_to_fit (自 C++11 起) | 通过释放未使用的内存来减少内存使用。 |
操作
公共 | clear | 清空内容。 |
公共 | insert | 插入字符。 |
公共 | erase | 删除字符。 |
公共 | push_back | 在末尾追加一个字符。 |
公共 | pop_back (自 C++11 起) | 移除最后一个字符。 |
公共 | append | 在末尾追加字符。 |
公共 | operator+= | 在末尾追加字符。 |
公共 | compare | 比较两个字符串。 |
公共 | starts_with (自 C++20 起) | 检查字符串是否以给定前缀开头。 |
公共 | ends_with (自 C++20 起) | 检查字符串是否以给定后缀结尾。 |
公共 | contains (自 C++23 起) | 检查字符串是否包含给定子字符串或字符。 |
公共 | replace | 替换字符串的指定部分。 |
公共 | substr | 返回一个子字符串。 |
公共 | copy | 复制字符。 |
公共 | resize | 改变存储的字符数。 |
公共 | resize_and_overwrite (自 C++23 起) | 改变存储的字符数,并可能通过用户提供的操作覆盖不确定的内容。 |
公共 | swap | 交换内容。 |
搜索
公共 | find | 在字符串中查找字符。 |
公共 | rfind | 查找子字符串的最后一次出现。 |
公共 | find_first_of | 查找字符的第一次出现。 |
公共 | find_first_not_of | 查找字符的第一次缺失。 |
公共 | find_last_of | 查找字符的最后一次出现。 |
公共 | find_last_not_of | 查找字符的最后一次缺失。 |
常量
公共静态constexpr | npos | 一个特殊值。确切的含义取决于上下文。 |
非成员函数
公共 | operator+ | 连接两个字符串或一个字符串和一个字符。 |
公共 | operator== operator!= (在 C++20 中移除) operator< (在 C++20 中移除) operator> (在 C++20 中移除) operator<= (在 C++20 中移除) operator>= (在 C++20 中移除) operator<=> | 按字典顺序比较两个字符串。 |
公共 | std::swap (std::basic_string) | std::swap 算法的重载。 |
公共 | erase (std::basic_string) erase_if (std::basic_string) |
|
输入/输出
公共 | operator<< operator>> | 对字符串执行流输入和输出。 |
公共 | getline | 从 I/O 流中读取数据到字符串。 |
数值转换
公共 | stoi stol stoll | 将字符串转换为有符号整数。 |
公共 | stoui stoul stoull | 将字符串转换为无符号整数。 |
公共 | stof stod stold | 将字符串转换为浮点值。 |
公共 | to_string | 将整型或浮点值转换为 `std::string`。 |
公共 | to_wstring | 将整型或浮点值转换为 `std::wstring`。 |
字面量
定义在命名空间 `std::literals::string_literals` 中公共 | operator ""s | 将字符数组字面量转换为 `std::string`。 |
辅助类
公共 | std::hash<std::string> (自 C++11 起) std::hash<std::u8string> (自 C++20 起) std::hash<std::u16string> (自 C++11 起) std::hash<std::u32string> (自 C++11 起) std::hash<std::wstring> (自 C++11 起) std::hash<std::pmr::string> (自 C++17 起) std::hash<std::pmr::u8string> (自 C++20 起) std::hash<std::pmr::u16string> (自 C++17 起) std::hash<std::pmr::u32string> (自 C++17 起) std::hash<std::pmr::wstring> (自 C++17 起) | 用于支持字符串哈希的 `std::hash` 特化。 |
推导指南 (C++17 起)
点击展开
推导指南
// (1)
template< class InputIt, class Alloc = std::allocator<
typename std::iterator_traits<InputIt>::value_type> >
basic_string( InputIt, InputIt, Alloc = Alloc() )
-> basic_string<typename std::iterator_traits<InputIt>::value_type,
std::char_traits<typename std::iterator_traits<InputIt>::value_type>,
Alloc>;
`(1)` 允许从迭代器范围推导
// (2)
template< class CharT,
class Traits,
class Alloc = std::allocator<CharT> >
explicit basic_string( std::basic_string_view<CharT, Traits>, const Alloc& = Alloc() )
-> basic_string<CharT, Traits, Alloc>;
// (3)
template< class CharT,
class Traits,
class Alloc = std::allocator<CharT>> >
basic_string( std::basic_string_view<CharT, Traits>, typename /*see below*/::size_type,
typename /*see below*/::size_type, const Alloc& = Alloc() )
-> basic_string<CharT, Traits, Alloc>;
`(2)` 和 `(3)` 允许从`std::basic_string_view
` 推导
`size_type` 参数类型指代由推导指南推导出的类型的 `size_type` 成员类型。
重载决议
为了使任何推导指南参与重载决议,必须满足以下要求
- 对于 `(1)`,`InputIt` 满足`LegacyInputIterator`
- 对于 `(1 - 3)`,`Alloc` 满足`Allocator`
库确定类型不满足 LegacyInputIterator
的程度是未指定的,但至少
- 整型不符合输入迭代器的条件。
同样,它确定类型不满足 Allocator
的程度是未指定的,但至少
- 成员类型
Alloc::value_type
必须存在。 - 当作为未求值操作数处理时,表达式
std::declval<Alloc&>().allocate(std::size_t{})
必须是格式良好的。
示例
基本操作
#include <iostream>
#include <string>
int main()
{
std::string s = "World";
std::cout << "Hello, " << s << "!" << std::endl;
}
Hello, World!
#include <iostream>
#include <string>
int main()
{
std::string a = "Hello, ";
std::string b = "World!";
std::string c = a + b;
std::cout << c;
}
Hello, World!
#include <iostream>
#include <string>
int main()
{
// Split by the comma.
// v
std::string a = "Hello, World!";
size_t pos = a.find(',');
std::string hello = a.substr(0, pos);
// 1 for a comma, and 1 for a space before "World"
std::string world = a.substr(pos + 1 + 1);
std::cout << hello << '\n' << world;
}
Hello
World!
转换
#include <iostream>
#include <string>
int main()
{
std::string numberInString = "8314";
// Note: stoi can throw exception if failed!
int number = std::stoi(numberInString);
std::cout << "Number: " << number;
}
Number: 8314
#include <iostream>
#include <string>
int main()
{
std::string numberInString = "3.141";
// Note: stof can throw exception if failed!
float number = std::stof(numberInString);
std::cout << "Number: " << number;
}
Number: 3.141
将数字转换为 `std::string`
#include <iostream>
#include <string>
int main()
{
std::string numberInString = "3.141";
// Note: stof can throw exception if failed!
float number = std::stof(numberInString);
std::cout << "Number: " << number;
}
s1: 123
s2: 456.200012
s3: 3.141590
如果您想要自定义格式化,请使用`std::stringstream`,或者非标准库fmtlib。