跳到主要内容
注意

请注意,本文尚未完成!您可以通过编辑此文档页面来提供帮助。

字符串视图类参考

概述

template< class CharT, /* ... */ >
class basic_string;

一个用作字符序列的轻量级视图的类。

内存

重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

别名

类型 std::string_view 实际上是以下类型的别名: std::basic_string_view<char>

可用的 std::basic_string_view 别名
pubstd::string_viewstd::basic_string_view<char>
pubstd::wstring_viewstd::basic_string_view<wchar_t>
pubstd::u8string_view (自 C++20 起)std::basic_string_view<char8_t>
pubstd::u16string_view (自 C++11 起)std::basic_string_view<char16_t>
pubstd::u32string_view (自 C++11 起)std::basic_string_view<char32_t>

技术细节

字符串视图的技术定义

类模板 basic_string_view 描述了一个对象,该对象可以引用一个常量连续的类字符对象序列,序列的第一个元素位于位置零。

典型的实现只包含两个成员:一个指向常量 CharT 的指针和一个大小。

std::basic_string_view 的每个特化都是一个可平凡复制的类型。 (自 C++11 起)
命名要求

std::basic_string_view 满足以下要求:

特性测试宏
重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

std::string_view

定义于string_view

模板参数

pubCharT字符类型
pub特性

CharTraits 类,指定字符类型上的操作。

重要

std::basic_string 类似,Traits::char_type 必须命名与 CharT 相同的类型,否则程序格式错误

.

类型名称

pubtraits_type特性
pubvalue_typeCharT
pubpointerCharT*
pubconst_pointerconst CharT*
pubreferenceCharT
pubconst_referenceconst CharT&
pubconst_iterator

实现定义的常量LegacyInputIteratorConstexprIterator  (自 C++20 起) LegacyContiguousIterator  (直至 C++20) contiguous_iterator  (自 C++20 起)value_typeCharT

pubconst_reverse_iteratorstd::reverse_iterator<const_iterator>
pubreverse_iteratorconst_reverse_iterator
pubsize_typestd::size_t
pubdifference_typestd::ptrdiff_t
迭代器

iteratorconst_iterator 是同一类型,因为字符串视图是常量字符序列的视图。

Container 的迭代器类型的所有要求也适用于 std::basic_string_viewiteratorconst_iterator 类型。

成员函数

pub(构造函数)

构造一个字符串视图。

pub(析构函数)

销毁字符串视图。

puboperator=

将值分配给字符串视图。

元素访问

pubat

访问指定字符带边界检查

puboperator[]

访问指定的字符。

pubfront

返回第一个字符。

pub末尾

返回最后一个字符。

pub数据

返回视图中第一个字符的指针。

迭代器

pubbegin
cbegin

返回指向起始元素的iterator/const_iterator

pubend
cend

返回指向结束元素的iterator/const_iterator

pubrbegin
crbegin

返回一个反向 iterator/const_iterator 指向起始位置。

pubrend
crend

返回一个反向 iterator/const_iterator 指向结束位置。

容量

pubempty

如果视图为空则返回 true,否则返回 false

pubsize
length

返回字符数。

pubmax_size

返回最大字符数。

修饰符

pubremove_prefix

通过向前移动其起始位置来缩小视图。

pubremove_suffix

通过向后移动其结束位置来缩小视图。

pubswap

交换内容。

操作

pubcopy

复制字符。

pubsubstr

返回子字符串。

pubcompare

比较两个视图。

pubstarts_with (自 C++20 起)

检查视图是否以给定前缀开头。

pubends_with (自 C++20 起)

检查视图是否以给定后缀结尾。

pubcontains (自 C++23 起)

检查视图是否包含给定子字符串或字符。

pubfind

查找子字符串的第一次出现。

pubrfind

查找子字符串的最后一次出现。

pubfind_first_of

查找字符的第一次出现。

pubfind_first_not_of

查找字符的第一次缺失。

pubfind_last_of

查找字符的最后一次出现。

pubfind_last_not_of

查找字符的最后一次缺失。

常量

pubstaticconstexprnpos

一个特殊值。具体含义取决于上下文。

非成员函数

puboperator==
operator!= (在 C++20 中移除)
operator< (在 C++20 中移除)
operator> (在 C++20 中移除)
operator<= (在 C++20 中移除)
operator>= (在 C++20 中移除)
operator<=>

字典序比较两个字符串视图。

输入/输出

puboperator<<

对视图执行流输出。

字面量

定义在命名空间 std::literals::string_view_literals
puboperator ""sv

从字符字面量创建字符串视图。

辅助类

pubstd::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 起)

std::hash 的特化,用于支持字符串视图哈希。

特化

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

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) 允许从任意范围推导字符类型。

重载决议

为了使任何推导指南参与重载决议,必须满足以下要求

示例

基本操作

创建和打印一个简单的字符串
#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!

转换

将字符串转换为 int
#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
将字符串转换为 float
#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

将字符串转换为 float
#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
本文源自 此 CppReference 页面。它可能为了改进或编辑偏好而被修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。
注意

请注意,本文尚未完成!您可以通过编辑此文档页面来提供帮助。

字符串视图类参考

概述

template< class CharT, /* ... */ >
class basic_string;

一个用作字符序列的轻量级视图的类。

内存

重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

别名

类型 std::string_view 实际上是以下类型的别名: std::basic_string_view<char>

可用的 std::basic_string_view 别名
pubstd::string_viewstd::basic_string_view<char>
pubstd::wstring_viewstd::basic_string_view<wchar_t>
pubstd::u8string_view (自 C++20 起)std::basic_string_view<char8_t>
pubstd::u16string_view (自 C++11 起)std::basic_string_view<char16_t>
pubstd::u32string_view (自 C++11 起)std::basic_string_view<char32_t>

技术细节

字符串视图的技术定义

类模板 basic_string_view 描述了一个对象,该对象可以引用一个常量连续的类字符对象序列,序列的第一个元素位于位置零。

典型的实现只包含两个成员:一个指向常量 CharT 的指针和一个大小。

std::basic_string_view 的每个特化都是一个可平凡复制的类型。 (自 C++11 起)
命名要求

std::basic_string_view 满足以下要求:

特性测试宏
重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

std::string_view

定义于string_view

模板参数

pubCharT字符类型
pub特性

CharTraits 类,指定字符类型上的操作。

重要

std::basic_string 类似,Traits::char_type 必须命名与 CharT 相同的类型,否则程序格式错误

.

类型名称

pubtraits_type特性
pubvalue_typeCharT
pubpointerCharT*
pubconst_pointerconst CharT*
pubreferenceCharT
pubconst_referenceconst CharT&
pubconst_iterator

实现定义的常量LegacyInputIteratorConstexprIterator  (自 C++20 起) LegacyContiguousIterator  (直至 C++20) contiguous_iterator  (自 C++20 起)value_typeCharT

pubconst_reverse_iteratorstd::reverse_iterator<const_iterator>
pubreverse_iteratorconst_reverse_iterator
pubsize_typestd::size_t
pubdifference_typestd::ptrdiff_t
迭代器

iteratorconst_iterator 是同一类型,因为字符串视图是常量字符序列的视图。

Container 的迭代器类型的所有要求也适用于 std::basic_string_viewiteratorconst_iterator 类型。

成员函数

pub(构造函数)

构造一个字符串视图。

pub(析构函数)

销毁字符串视图。

puboperator=

将值分配给字符串视图。

元素访问

pubat

访问指定字符带边界检查

puboperator[]

访问指定的字符。

pubfront

返回第一个字符。

pub末尾

返回最后一个字符。

pub数据

返回视图中第一个字符的指针。

迭代器

pubbegin
cbegin

返回指向起始元素的iterator/const_iterator

pubend
cend

返回指向结束元素的iterator/const_iterator

pubrbegin
crbegin

返回一个反向 iterator/const_iterator 指向起始位置。

pubrend
crend

返回一个反向 iterator/const_iterator 指向结束位置。

容量

pubempty

如果视图为空则返回 true,否则返回 false

pubsize
length

返回字符数。

pubmax_size

返回最大字符数。

修饰符

pubremove_prefix

通过向前移动其起始位置来缩小视图。

pubremove_suffix

通过向后移动其结束位置来缩小视图。

pubswap

交换内容。

操作

pubcopy

复制字符。

pubsubstr

返回子字符串。

pubcompare

比较两个视图。

pubstarts_with (自 C++20 起)

检查视图是否以给定前缀开头。

pubends_with (自 C++20 起)

检查视图是否以给定后缀结尾。

pubcontains (自 C++23 起)

检查视图是否包含给定子字符串或字符。

pubfind

查找子字符串的第一次出现。

pubrfind

查找子字符串的最后一次出现。

pubfind_first_of

查找字符的第一次出现。

pubfind_first_not_of

查找字符的第一次缺失。

pubfind_last_of

查找字符的最后一次出现。

pubfind_last_not_of

查找字符的最后一次缺失。

常量

pubstaticconstexprnpos

一个特殊值。具体含义取决于上下文。

非成员函数

puboperator==
operator!= (在 C++20 中移除)
operator< (在 C++20 中移除)
operator> (在 C++20 中移除)
operator<= (在 C++20 中移除)
operator>= (在 C++20 中移除)
operator<=>

字典序比较两个字符串视图。

输入/输出

puboperator<<

对视图执行流输出。

字面量

定义在命名空间 std::literals::string_view_literals
puboperator ""sv

从字符字面量创建字符串视图。

辅助类

pubstd::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 起)

std::hash 的特化,用于支持字符串视图哈希。

特化

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

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) 允许从任意范围推导字符类型。

重载决议

为了使任何推导指南参与重载决议,必须满足以下要求

示例

基本操作

创建和打印一个简单的字符串
#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!

转换

将字符串转换为 int
#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
将字符串转换为 float
#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

将字符串转换为 float
#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
本文源自 此 CppReference 页面。它可能为了改进或编辑偏好而被修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。