请注意,本文尚未完成!您可以通过编辑此文档页面来提供帮助。
字符串视图类参考
概述
- 简化
- 详细
template< class CharT, /* ... */ >
class basic_string;
template<
class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
>
class basic_string;
一个用作字符序列的轻量级视图的类。
内存
本节需要改进。您可以通过编辑此文档页面来帮助我们。
别名
类型 std::string_view
实际上是以下类型的别名: std::basic_string_view<char>
。
可用的 std::basic_string_view
别名
pub | std::string_view | std::basic_string_view<char> |
pub | std::wstring_view | std::basic_string_view<wchar_t> |
pub | std::u8string_view (自 C++20 起) | std::basic_string_view<char8_t> |
pub | std::u16string_view (自 C++11 起) | std::basic_string_view<char16_t> |
pub | std::u32string_view (自 C++11 起) | std::basic_string_view<char32_t> |
技术细节
字符串视图的技术定义
类模板 basic_string_view
描述了一个对象,该对象可以引用一个常量连续的类字符对象序列,序列的第一个元素位于位置零。
典型的实现只包含两个成员:一个指向常量 CharT
的指针和一个大小。
std::basic_string_view
的每个特化都是一个可平凡复制的
类型。 (自 C++11 起)命名要求
std::basic_string_view
满足以下要求:
-
可平凡复制的 (自 C++23 起)
-
注意
即使在 C++23 中引入正式要求之前,
std::basic_string_view
的特化在所有现有实现中都已经是可以平凡复制的类型。
-
特性测试宏
本节需要改进。您可以通过编辑此文档页面来帮助我们。
std::string_view
定义于 | string_view |
模板参数
pub | CharT | 字符类型 |
pub | 特性 |
重要 与 |
类型名称
pub | traits_type | 特性 |
pub | value_type | CharT |
pub | pointer | CharT* |
pub | const_pointer | const CharT* |
pub | reference | CharT |
pub | const_reference | const CharT& |
pub | const_iterator | 实现定义的常量 |
pub | const_reverse_iterator | std::reverse_iterator<const_iterator> |
pub | reverse_iterator | const_reverse_iterator |
pub | size_type | std::size_t |
pub | difference_type | std::ptrdiff_t |
iterator
和 const_iterator
是同一类型,因为字符串视图是常量字符序列的视图。
Container
的迭代器类型的所有要求也适用于 std::basic_string_view
的 iterator
和 const_iterator
类型。
成员函数
pub | (构造函数) | 构造一个字符串视图。 |
pub | (析构函数) | 销毁字符串视图。 |
pub | operator= | 将值分配给字符串视图。 |
元素访问
pub | at | 访问指定字符带边界检查。 |
pub | operator[] | 访问指定的字符。 |
pub | front | 返回第一个字符。 |
pub | 末尾 | 返回最后一个字符。 |
pub | 数据 | 返回视图中第一个字符的指针。 |
迭代器
pub | begin cbegin | 返回指向起始元素的 |
pub | end cend | 返回指向结束元素的 |
pub | rbegin crbegin | 返回一个反向 |
pub | rend crend | 返回一个反向 |
容量
pub | empty | 如果视图为空则返回 |
pub | size length | 返回字符数。 |
pub | max_size | 返回最大字符数。 |
修饰符
pub | remove_prefix | 通过向前移动其起始位置来缩小视图。 |
pub | remove_suffix | 通过向后移动其结束位置来缩小视图。 |
pub | swap | 交换内容。 |
操作
pub | copy | 复制字符。 |
pub | substr | 返回子字符串。 |
pub | compare | 比较两个视图。 |
pub | starts_with (自 C++20 起) | 检查视图是否以给定前缀开头。 |
pub | ends_with (自 C++20 起) | 检查视图是否以给定后缀结尾。 |
pub | contains (自 C++23 起) | 检查视图是否包含给定子字符串或字符。 |
pub | find | 查找子字符串的第一次出现。 |
pub | rfind | 查找子字符串的最后一次出现。 |
pub | find_first_of | 查找字符的第一次出现。 |
pub | find_first_not_of | 查找字符的第一次缺失。 |
pub | find_last_of | 查找字符的最后一次出现。 |
pub | find_last_not_of | 查找字符的最后一次缺失。 |
常量
pubstaticconstexpr | npos | 一个特殊值。具体含义取决于上下文。 |
非成员函数
pub | operator== operator!= (在 C++20 中移除) operator< (在 C++20 中移除) operator> (在 C++20 中移除) operator<= (在 C++20 中移除) operator>= (在 C++20 中移除) operator<=> | 字典序比较两个字符串视图。 |
输入/输出
pub | operator<< | 对视图执行流输出。 |
字面量
定义在命名空间std::literals::string_view_literals
中pub | operator ""sv | 从字符字面量创建字符串视图。 |
辅助类
pub | std::hash<std::string_view> (自 C++11 起) std::hash<std::wstring_view> (自 C++11 起) std::hash<std::u8string_view> (自 C++20 起) std::hash<std::u16string_view> (自 C++11 起) std::hash<std::u32string_view> (自 C++11 起) |
|
特化
- 自 C++20 起
template<class CharT, class Traits>
inline constexpr bool ranges::enable_borrowed_range<std::basic_string_view<CharT, Traits>> = true;
此ranges::enable_borrowed_range
的特化使basic_string_view
满足borrowed_range
。
- 自 C++20 起
template<class CharT, class Traits>
inline constexpr bool ranges::enable_view<std::basic_string_view<CharT, Traits>> = true;
此ranges::enable_view
的特化使basic_string_view
满足view
。
推导指南(自 C++20 起)
点击展开
// (1)
template<class It, class End>
basic_string_view(It, End) -> basic_string_view<std::iter_value_t<It>>;
(1)
允许从迭代器-哨兵对推导。
// (2)
template<class R>
basic_string_view(R&&) -> basic_string_view<ranges::range_value_t<R>>;
(2)
允许从任意范围推导字符类型。
重载决议
为了使任何推导指南参与重载决议,必须满足以下要求
- 对于
(1)
,It
满足contiguous_iterator
,并且End
满足It
的sized_sentinel_for
- 对于
(2)
,R
满足contiguous_range
示例
基本操作
#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