类模板 basic_string_view<charT> 已添加到 C++17 中,用作函数接受各种不相关的字符串类型的安全高效方法,而无需对这些类型进行模板化。 该类包含指向连续字符数据序列的非所属指针,以及指定序列中字符数的长度。 关于序列是否以 null 结尾,没有做出任何假设。
标准库根据元素类型定义多个专用化:
string_viewwstring_viewu16string_viewu32string_view
basic_string_view 描述读取字符串数据所需的最小通用接口。 它提供对基础数据的常量访问;不会进行复制(除了 copy 函数)。 数据在任何位置都可能包含或不包含 null 值 (\0)。
basic_string_view 无法控制对象的生存期。 调用方有责任确保基础字符串数据的有效性。
接受 string_view 类型参数的函数可用于处理任何类字符串类型,而无需将函数设置为模板,或将函数限制为字符串类型的特定子集。 唯一的要求是存在从字符串类型到 string_view 的隐式转换。 所有标准字符串类型都隐式转换为 string_view,后者包含相同的元素类型。 换句话说,std::string 可转换为 string_view,但不能转换为 wstring_view。
下面的示例演示采用 f 类型参数的非模板函数 wstring_view。 可以使用 std::wstring、wchar_t* 和 winrt::hstring 类型参数调用它。
// compile with: /std:c++17
// string_view that uses elements of wchar_t
void f(wstring_view);
// pass a std::wstring:
const std::wstring& s { L"Hello" };
f(s);
// pass a C-style null-terminated string (string_view is not null-terminated):
const wchar_t* ns = L"Hello";
f(ns);
// pass a C-style character array of len characters (excluding null terminator):
const wchar_t* cs { L"Hello" };
size_t len { 5 };
f({cs,len});
// pass a WinRT string
winrt::hstring hs { L"Hello" };
f(hs);
Syntax
template <class CharType, class Traits = char_traits<CharType>>
class basic_string_view;
Parameters
CharType
存储在 basic_string_view 中的字符类型。 C++ 标准库提供以下 typedef,用于实现此模板的专用化。
-
string_view(用于char类型元素) -
wstring_view(用于wchar_t) - 若
u16string_view,表示集char16_t -
u32string_view(对于char32_t)。
Traits
默认为 char_traits<CharType>。
Constructors
| Constructor | Description |
|---|---|
basic_string_view |
构造一个为空的 basic_string_view,或者指向所有或部分其他字符串对象数据,或指向 C 样式字符数组。 |
Typedefs
| Type name | Description |
|---|---|
const_iterator |
可读取 const 元素的随机访问迭代器。 |
const_pointer |
using const_pointer = const value_type*; |
const_reference |
using const_reference = const value_type&; |
const_reverse_iterator |
using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
difference_type |
using difference_type = ptrdiff_t; |
iterator |
using iterator = const_iterator; |
npos |
static constexpr size_type npos = size_type(-1); |
pointer |
using pointer = value_type*; |
reference |
using reference = value_type&; |
reverse_iterator |
using reverse_iterator = const_reverse_iterator; |
size_type |
using size_type = size_t; |
traits_type |
using traits_type = Traits; |
value_type |
using value_type = CharType; |
Member operators
| Operator | Description |
|---|---|
operator= |
将 basic_string_view 或可转换字符串对象分配给另一个 basic_string_view。 |
operator[] |
返回指定索引处的元素。 |
Member functions
| Member function | Description |
|---|---|
at |
返回对指定位置的元素的 const_reference。 |
back |
返回对最后一个元素的 const_reference。 |
begin |
返回确定第一个元素位置的 const 迭代器。 (basic_string_view 是不可变的。) |
cbegin |
与 begin 相同。 |
cend |
返回指向最后一个元素下一位置的 const 迭代器。 |
copy |
将指定数目的字符从源 basic_string_view 中的索引位置复制到目标字符数组。 (不建议这样做。请改用 _Copy_s。) |
_Copy_s |
确保 CRT 复制函数安全。 |
compare |
将 basic_string_view 与指定 basic_string_view 比较,确定它们是否相等或按字典顺序一个字符串是否小于另一个。 |
crbegin |
与 rbegin 相同。 |
crend |
与 rend 相同。 |
data |
返回指向字符序列的原始非所属指针。 |
empty |
测试 basic_string_view 是否包含字符。 |
end |
与 cend 相同。 |
ends_with
C++20 |
检查字符串视图是否以指定后缀结尾。 |
find |
向前搜索与指定字符序列匹配的第一个子字符串。 |
find_first_not_of |
搜索不属于指定 basic_string_view 或可转换字符串对象的任何元素的第一个字符。 |
find_first_of |
搜索与指定 basic_string_view 或可转换字符串对象的任何元素匹配的第一个字符。 |
find_last_not_of |
搜索不属于指定 basic_string_view 或可转换字符串对象的任何元素的最后一个字符。 |
find_last_of |
搜索属于指定 basic_string_view 或可转换字符串对象的元素的最后一个字符。 |
front |
返回对第一个元素的 const_reference。 |
length |
返回当前元素数目。 |
max_size |
返回 basic_string_view 可包含的最大字符数。 |
rbegin |
返回确定反向 const 中第一个元素位置的 basic_string_view 迭代器。 |
remove_prefix |
将指针向前移动指定数量的元素。 |
remove_suffix |
按照从后面开始的指定元素数来减小视图大小。 |
rend |
返回指向反向 const 中最后一个元素下一位置的 basic_string_view 迭代器。 |
rfind |
反向搜索 basic_string_view,获取与指定字符序列匹配的第一个子字符串。 |
size |
返回当前元素数目。 |
starts_with
C++20 |
检查字符串视图是否以给定前缀开头。 |
substr |
返回从指定索引开始的指定长度的子字符串。 |
swap |
交换两个 basic_string_view 对象的内容。 |
Remarks
如果要求函数生成的序列长于 max_size 元素,这个函数将通过引发 length_error 类型的对象来报告长度错误。
Requirements
/std:c++17 或更高版本。
Header:<string_view>
Namespace:std
basic_string_view::at
返回对指定从 0 开始的索引处的字符的 const_reference。
constexpr const_reference at(size_type offset) const;
Parameters
offset
要引用的元素的索引。
Return value
对由参数索引指定的位置的字符的 const_reference。
Remarks
第一个元素的索引为零,其后续元素以正整数进行连续地索引,因此,长度为 basic_string_view 的 n 具有由数字 n 索引的第 *n - 1* 个元素。 对于无效索引,at 会引发异常,这与 operator[] 不同。
一般情况下,我们建议对 at 这样的序列使用 std::vector,永远不要使用 basic_string_view。 传递给序列的无效索引是一个逻辑错误,应在开发期间发现和修复。 如果某个程序不确定其索引是否有效,则应测试它们,而不是调用 at() 并依赖异常来防范草率编程。
有关详细信息,请参阅 basic_string_view::operator[]。
Example
// basic_string_view_at.cpp
// compile with: /EHsc
#include <string_view>
#include <iostream>
int main()
{
using namespace std;
const string_view str1("Hello world");
string_view::const_reference refStr2 = str1.at(8); // 'r'
}
basic_string_view::back
返回对最后一个元素的 const_reference。
constexpr const_reference back() const;
Return value
const_reference 中最后一个元素的 basic_string_view。
Remarks
如果 basic_string_view 为空,则会引发异常。
请记住,在修改 basic_string_view 后(例如通过调用 remove_suffix),此函数返回的元素将不再是基础数据中的最后一个元素。
Example
使用 C 字符串文本构造的 string_view 不包括终止 null。 因此以下示例中,back 返回 'p',而不是 '\0'。
char c[] = "Help"; // char[5]
string_view sv{ c };
cout << sv.size(); // size() == 4
cout << sv.back() << endl; // p
嵌入的 null 被视为任何其他字符:
string_view e = "embedded\0nulls"sv;
cout << boolalpha << (e.back() == 's'); // true
basic_string_view::basic_string_view
构造一个 basic_string_view。
constexpr basic_string_view() noexcept;
constexpr basic_string_view(const basic_string_view&) noexcept = default;
constexpr basic_string_view(const charT* str);
constexpr basic_string_view(const charT* str, size_type len);
Parameters
str
指向字符值的指针。
len
要包含在视图中的字符数。
Remarks
具有 charT* 参数的构造函数假定输入以 null 结尾,但终止 null 不包含在 basic_string_view 中。
还可以使用文本构造 basic_string_view。 请参阅 operator"" sv。
basic_string_view::begin
与 cbegin 相同。
constexpr const_iterator begin() const noexcept;
Return value
返回确定第一个元素位置的 const_iterator。
basic_string_view::cbegin
返回确定范围中第一个元素位置的 const_iterator。
constexpr const_iterator cbegin() const noexcept;
Return value
const 随机访问迭代器,指向范围的第一个元素,或刚超出空范围末尾的位置(对于空范围,cbegin() == cend())。
basic_string_view::cend
返回确定刚超出范围中最后一个元素的位置的 const_iterator。
constexpr const_iterator cend() const noexcept;
Return value
指向刚超出范围末尾的位置的 const 随机访问迭代器。
Remarks
不应对 cend 返回的值取消引用。
basic_string_view::compare
与指定 basic_string_view(或可转换字符串类型)进行区分大小写比较,以确定两个对象是否相等或按字典顺序一个对象是否小于另一个。
<string_view> 运算符使用此成员函数进行比较。
constexpr int compare(basic_string_view strv) const noexcept;
constexpr int compare(size_type pos, size_type num, basic_string_view strv) const;
constexpr int compare(size_type pos, size_type num, basic_string_view strv, size_type offset, size_type num2) const;
constexpr int compare(const charT* ptr) const;
constexpr int compare(size_type pos, size_type num, const charT* ptr) const;
constexpr int compare(size_type pos, size_type num, const charT* ptr, size_type num2) const;
Parameters
strv
要与此 basic_string_view 进行比较的 basic_string_view。
pos
开始进行比较的 basic_string_view 的索引。
num
要比较的 basic_string_view 的最大字符数。
num2
要比较的 strv 的最大字符数。
offset
开始进行比较的 strv 的索引。
ptr
要与此 basic_string_view 进行比较的 C 字符串。
Return value
- 如果此
basic_string_view小于strv或ptr,则为负值 - 如果两个字符序列相等,则为零
- 如果此
basic_string_view大于strv或ptr,则为正值
Remarks
compare 成员函数对每个字符序列的所有或部分执行区分大小写的比较。
Example
// basic_string_view_compare.cpp
// compile with: /EHsc
#include <string_view>
#include <iostream>
#include <string>
using namespace std;
string to_alpha(int result)
{
if (result < 0) return " less than ";
else if (result == 0) return " equal to ";
else return " greater than ";
}
int main()
{
// The first member function compares
// two string_views
string_view sv_A("CAB");
string_view sv_B("CAB");
cout << "sv_A is " << sv_A << endl;
cout << "sv_B is " << sv_B << endl;
int comp1 = sv_A.compare(sv_B);
cout << "sv_A is" << to_alpha(comp1) << "sv_B.\n";
// The second member function compares part of
// an operand string_view to another string_view
string_view sv_C("AACAB");
string_view sv_D("CAB");
cout << "sv_C is: " << sv_C << endl;
cout << "sv_D is: " << sv_D << endl;
int comp2a = sv_C.compare(2, 3, sv_D);
cout << "The last three characters of sv_C are"
<< to_alpha(comp2a) << "sv_D.\n";
int comp2b = sv_C.compare(0, 3, sv_D);
cout << "The first three characters of sv_C are"
<< to_alpha(comp2b) << "sv_D.\n";
// The third member function compares part of
// an operand string_view to part of another string_view
string_view sv_E("AACAB");
string_view sv_F("DCABD");
cout << "sv_E: " << sv_E << endl;
cout << "sv_F is: " << sv_F << endl;
int comp3a = sv_E.compare(2, 3, sv_F, 1, 3);
cout << "The three characters from position 2 of sv_E are"
<< to_alpha(comp3a)
<< "the 3 characters of sv_F from position 1.\n";
// The fourth member function compares
// an operand string_view to a C string
string_view sv_G("ABC");
const char* cs_A = "DEF";
cout << "sv_G is: " << sv_G << endl;
cout << "cs_A is: " << cs_A << endl;
int comp4a = sv_G.compare(cs_A);
cout << "sv_G is" << to_alpha(comp4a) << "cs_A.\n";
// The fifth member function compares part of
// an operand string_view to a C string
string_view sv_H("AACAB");
const char* cs_B = "CAB";
cout << "sv_H is: " << sv_H << endl;
cout << "cs_B is: " << cs_B << endl;
int comp5a = sv_H.compare(2, 3, cs_B);
cout << "The last three characters of sv_H are"
<< to_alpha(comp5a) << "cs_B.\n";
// The sixth member function compares part of
// an operand string_view to part of an equal length of
// a C string
string_view sv_I("AACAB");
const char* cs_C = "ACAB";
cout << "sv_I is: " << sv_I << endl;
cout << "cs_C: " << cs_C << endl;
int comp6a = sv_I.compare(1, 3, cs_C, 3);
cout << "The 3 characters from position 1 of sv_I are"
<< to_alpha(comp6a) << "the first 3 characters of cs_C.\n";
}
sv_A is CAB
sv_B is CAB
sv_A is equal to sv_B.
sv_C is: AACAB
sv_D is: CAB
The last three characters of sv_C are equal to sv_D.
The first three characters of sv_C are less than sv_D.
sv_E: AACAB
sv_F is: DCABD
The three characters from position 2 of sv_E are equal to the 3 characters of sv_F from position 1.
sv_G is: ABC
cs_A is: DEF
sv_G is less than cs_A.
sv_H is: AACAB
cs_B is: CAB
The last three characters of sv_H are equal to cs_B.
sv_I is: AACAB
cs_C: ACAB
The 3 characters from position 1 of sv_I are equal to the first 3 characters of cs_C.
basic_string_view::copy
将指定数目的字符从源 basic_string_view 中的索引位置复制到目标字符数组。 建议改用安全函数 basic_string_view::_Copy_s。
size_type copy(charT* ptr, size_type count, size_type offset = 0) const;
Parameters
ptr
要复制的元素的目标字符数组。
count
要从源 basic_string_view 复制的最大字符数。
offset
要进行复制的源 basic_string_view 中的开始位置。
Return value
复制的字符数。
Remarks
空字符不追加到副本的末尾。
basic_string_view::_Copy_s
使用安全 CRT 复制函数,而不是 copy。
size_type _Copy_s(
value_type* dest,
size_type dest_size,
size_type count,
size_type _Off = 0) const;
Parameters
dest
要复制的元素的目标字符数组。
dest_size
dest 的大小。
count 要从源字符串复制的字符的最大数目。
_Off
要进行复制的源字符串中的开始位置。
Return value
复制的字符数。
Remarks
空字符不追加到副本的末尾。
For more information, see c-runtime-library/security-features-in-the-crt.
basic_string_view::crbegin
返回确定反向 const_reverse_iterator 中第一个元素位置的 basic_string_view。
constexpr const_reverse_iterator crbegin() const noexcept;
Return value
确定反向 const_reverse_iterator 中第一个元素位置的 basic_string_view。
basic_string_view::crend
与 rend 相同。
constexpr const_reverse_iterator crend() const noexcept;
Return value
返回确定反向 const_reverse_iterator 末尾下一位置的 basic_string_view。
basic_string_view::data
返回原始非所属指针,它指向用于构造 basic_string_view 的对象的常量字符序列。
constexpr value_type *data() const noexcept;
Return value
指向字符序列中第一个元素的指向常量的指针。
Remarks
该指针无法修改字符。
basic_string_view 字符序列不一定以 null 结尾。
data 的返回类型不是有效的 C 字符串,因为未追加空字符。 空字符 \0 在 basic_string_view 类型的对象中没有特殊含义,可以像其他字符一样成为 basic_string_view 对象的一部分。
basic_string_view::empty
测试 basic_string_view 是否包含字符。
constexpr bool empty() const noexcept;
Return value
如果 true 对象不包含字符,则为 basic_string_view;如果它至少包含一个字符,则为 false。
Remarks
成员函数等效于 size() == 0。
basic_string_view::end
返回指向最后一个元素下一位置的随机访问 const_iterator。
constexpr const_iterator end() const noexcept;
Return value
返回指向最后一个元素下一位置的随机访问 const_iterator。
Remarks
end 用于测试 const_iterator 是否已到达其 basic_string_view 的末尾。 不应对 end 返回的值取消引用。
basic_string_view::ends_with
检查字符串视图是否以指定后缀结尾。
bool ends_with(const CharType c) const noexcept;
bool ends_with(const CharType* const x) const noexcept;
bool ends_with(const basic_string_view sv) const noexcept;
Parameters
c
要查找的单个字符后缀。
sv
包含要查找的后缀的字符串视图。
可以传递 std::basic_string,它会转换为 basic_string_view。
x
包含要查找的后缀的以 null 结尾的字符字符串。
Return value
如果字符串视图以指定后缀结尾,则为 true;否则为 false。
Remarks
ends_with() 是 C++20 中的新增功能。 若要使用它,请指定 /std:c++20 或更高版本编译器选项。
查看 starts_with 以检查字符串视图是否以指定前缀开头。
Example
// Requires /std:c++20 or later
#include <string>
#include <iostream>
int main()
{
std::cout << std::boolalpha; // so booleans show as 'true'/'false'
std::cout << std::string_view("abcdefg").ends_with('g') << '\n';
std::cout << std::string_view("abcdefg").ends_with("eFg") << '\n';
std::basic_string<char> str2 = "efg";
std::cout << std::string_view("abcdefg").ends_with(str2);
return 0;
}
true
false
true
basic_string_view::find
向前搜索 basic_string_view,搜索与指定字符序列匹配的第一个字符或子字符串。
constexpr size_type find(basic_string_view str, size_type offset = 0) const noexcept;
constexpr size_type find(charT chVal, size_type offset = 0) const noexcept;
constexpr size_type find(const charT* ptr, size_type offset, size_type count) const;
constexpr size_type find(const charT* ptr, size_type offset = 0) const;
Parameters
str
成员函数要搜索的 basic_string_view。
chVal
成员函数要搜索的字符值。
offset
在此处开始搜索的索引。
ptr
成员函数要搜索的 C 字符串。
count
ptr 中的字符数,从第一个字符开始向前计数。
Return value
搜索成功时,则为搜索的子字符串的首个字符的索引;否则为 npos。
basic_string_view::find_first_not_of
搜索不属于指定 basic_string_view 或可转换字符串对象的元素的第一个字符。
constexpr size_type find_first_not_of(basic_string_view str, size_type offset = 0) const noexcept;
constexpr size_type find_first_not_of(charT chVal, size_type offset = 0) const noexcept;
constexpr size_type find_first_not_of(const charT* ptr, size_type offset, size_type count) const;
constexpr size_type find_first_not_of(const charT* ptr, size_type offset = 0) const;
Parameters
str
成员函数要搜索的 basic_string_view。
chVal
成员函数要搜索的字符值。
offset
在此处开始搜索的索引。
ptr
成员函数要搜索的 C 字符串。
count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。
Return value
搜索成功时,则为搜索的子字符串的首个字符的索引;否则为 npos。
basic_string_view::find_first_of
搜索与指定 basic_string_view 中任何元素匹配的第一个字符。
constexpr size_type find_first_of(basic_string_view str, size_type offset = 0) const noexcept;
constexpr size_type find_first_of(charT chVal, size_type offset = 0) const noexcept;
constexpr size_type find_first_of(const charT* str, size_type offset, size_type count) const;
constexpr size_type find_first_of(const charT* str, size_type offset = 0) const;
Parameters
chVal
成员函数要搜索的字符值。
offset
在此处开始搜索的索引。
ptr
成员函数要搜索的 C 字符串。
count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。
str
成员函数要搜索的 basic_string_view。
Return value
搜索成功时,则为搜索的子字符串的首个字符的索引;否则为 npos。
basic_string_view::find_last_not_of
搜索不属于指定 basic_string_view 的任何元素的最后一个字符。
constexpr size_type find_last_not_of(basic_string_view str, size_type offset = npos) const noexcept;
constexpr size_type find_last_not_of(charT chVal, size_type offset = npos) const noexcept;
constexpr size_type find_last_not_of(const charT* ptr, size_type offset, size_type count) const;
constexpr size_type find_last_not_of(const charT* ptr, size_type offset = npos) const;
Parameters
str
成员函数要搜索的 basic_string_view。
chVal
成员函数要搜索的字符值。
offset
在此处结束搜索的索引。
ptr
成员函数要搜索的 C 字符串。
count
ptr 中的字符数,从第一个字符开始向前计数。
Return value
搜索成功时,则为搜索的子字符串的首个字符的索引;否则为 string_view::npos。
basic_string_view::find_last_of
搜索与指定 basic_string_view 中任何元素匹配的最后一个字符。
constexpr size_type find_last_of(basic_string_view str, size_type offset = npos) const noexcept;
constexpr size_type find_last_of(charT chVal, size_type offset = npos) const noexcept;
constexpr size_type find_last_of(const charT* ptr, size_type offset, size_type count) const;
constexpr size_type find_last_of(const charT* ptr, size_type offset = npos) const;
Parameters
str
成员函数要搜索的 basic_string_view。
chVal
成员函数要搜索的字符值。
offset
在此处结束搜索的索引。
ptr
成员函数要搜索的 C 字符串。
count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。
Return value
搜索成功时,则为搜索的子字符串的最后一个字符的索引;否则为 npos。
basic_string_view::front
返回对第一个元素的 const_reference。
constexpr const_reference front() const;
Return value
第一个元素的 const_reference。
Remarks
如果 basic_string_view 为空,则会引发异常。
basic_string_view::length
返回当前元素数目。
constexpr size_type length() const noexcept;
Remarks
成员函数与 size 相同。
basic_string_view::max_size
返回 basic_string_view 可包含的最大字符数。
constexpr size_type max_size() const noexcept;
Return value
返回 basic_string_view 可包含的最大字符数。
Remarks
当运算生成长度大于 length_error 的 basic_string_view 时,将引发 max_size() 类型异常。
basic_string_view::operator=
将 basic_string_view 或可转换字符串对象分配给另一个 basic_string_view。
constexpr basic_string_view& operator=(const basic_string_view&) noexcept = default;
Example
string_view s = "Hello";
string_view s2 = s;
basic_string_view::operator[]
使用指定索引提供对字符的 const_reference。
constexpr const_reference operator[](size_type offset) const;
Parameters
offset
要引用的元素的索引。
Return value
对由参数索引指定的位置的字符的 const_reference。
Remarks
第一个元素的索引为零,其后续元素以正整数进行连续索引,因此,长度为 basic_string_view 的 n 具有由数字 n 索引的第 *n-1* 个元素。
在提供对 operator[] 元素的读取访问权限时,at 比成员函数 basic_string_view 更快。
operator[] 不检查作为参数传递的索引是否有效。 传递给 operator[] 无效的索引会导致未定义的行为。
如果基础字符串数据被所属对象修改或删除,则返回的引用可能会失效。
在 _ITERATOR_DEBUG_LEVEL 设置为 1 或 2 的情况下进行编译时,如果尝试访问 basic_string_view 边界以外的元素,将发生运行时错误。 For more information, see Checked Iterators.
basic_string_view::rbegin
返回指向反向 const 中第一个元素的 basic_string_view 迭代器。
constexpr const_reverse_iterator rbegin() const noexcept;
Return value
返回指向反向 basic_string_view 中第一个元素的随机访问迭代器,用于确定相应非反向 basic_string_view 中最后一个元素的位置。
Remarks
rbegin 用于反向 basic_string_view,正如 begin 用于 basic_string_view 一样。
rbegin 可用于向后初始化迭代。
basic_string_view::remove_prefix
将指针向前移动指定数量的元素。
constexpr void remove_prefix(size_type n);
Remarks
使基础数据保持不变。 将 basic_string_view 指针向前移动 n 个元素,并将专用 size 数据成员设置为 size - n。
basic_string_view::remove_suffix
按照从后面开始的指定元素数来减小视图大小。
constexpr void remove_suffix(size_type n);
Remarks
使基础数据和指向它的指针保持不变。 将专用 size 数据成员设置为 size - n。
basic_string_view::rend
返回指向反向 const 中最后一个元素下一位置的 basic_string_view 迭代器。
constexpr reverse_iterator rend() const noexcept;
Return value
指向反向 const 中最后一个元素下一位置的 basic_string_view 反向随机访问迭代器。
Remarks
rend 用于反向 basic_string_view,正如 end 用于 basic_string_view 一样。
rend 可用于测试反向迭代器是否已到达其 basic_string_view 末尾。 不应对 rend 返回的值取消引用。
basic_string_view::rfind
反向搜索 basic_string_view,获取与指定字符序列匹配的子字符串。
constexpr size_type rfind(basic_string_view str, size_type offset = npos) const noexcept;
constexpr size_type rfind(charT chVal, size_type offset = npos) const noexcept;
constexpr size_type rfind(const charT* ptr, size_type offset, size_type count) const;
constexpr size_type rfind(const charT* ptr, size_type offset = npos) const;
Parameters
chVal
成员函数要搜索的字符值。
offset
在此处开始搜索的索引。
ptr
成员函数要搜索的 C 字符串。
count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。
str
成员函数要搜索的 basic_string_view。
Return value
搜索成功时,则为子字符串的首个字符的索引;否则为 npos。
basic_string_view::size
返回 basic_string_view 中的元素数量。
constexpr size_type size() const noexcept;
Return value
basic_string_view 的长度。
Remarks
basic_string_view 可以修改其长度,例如通过 remove_prefix 和 remove_suffix。 由于这不会修改基础字符串数据,因此 basic_string_view 大小不一定是基础数据的大小。
basic_string_view::starts_with
检查字符串视图是否以指定前缀开始。
bool starts_with(const CharType c) const noexcept;
bool starts_with(const CharType* const x) const noexcept;
bool starts_with(const basic_string_view sv) const noexcept;
Parameters
c
要查找的单个字符前缀。
sv
包含要查找的前缀的字符串视图。
可以传递 std::basic_string,其将转换为字符串视图。
x
包含要查找的前缀的以 null 结尾的字符字符串。
Return value
如果字符串以指定的前缀开始,则为 true;否则为 false。
Remarks
starts_with() 是 C++20 中的新增功能。 若要使用它,请指定 /std:c++20 或更高版本编译器选项。
请参见 ends_with 以查看字符串是否以后缀结尾。
Example
// Requires /std:c++20 or later
#include <string>
#include <iostream>
int main()
{
std::cout << std::boolalpha; // so booleans show as 'true'/'false'
std::cout << std::string_view("abcdefg").starts_with('b') << '\n';
std::cout << std::string_view("abcdefg").starts_with("aBc") << '\n';
std::basic_string<char> str2 = "abc";
std::cout << std::string_view("abcdefg").starts_with(str2);
return 0;
}
false
false
true
basic_string_view::substr
返回 basic_string_view,表示指定位置的(最大)指定字符数。
constexpr basic_string_view substr(size_type offset = 0, size_type count = npos) const;
Parameters
offset
从进行复制所在的位置查找元素的索引,默认值为 0。
count
子字符串中要包含的字符数(如果存在)。
Return value
一个 basic_string_view 对象,表示元素的指定子序列。
basic_string_view::swap
交换两个 basic_string_view,也就是指向基础字符串数据的指针和大小值。
constexpr void swap(basic_string_view& sv) noexcept;
Parameters
sv
要与目标 basic_string_view 值交换其指针和大小值的源 basic_string_view。