basic_string_view

类模板 basic_string_view<charT> 已添加到 C++17 中,用作函数接受各种不相关的字符串类型的安全高效方法,而无需对这些类型进行模板化。 该类包含指向连续字符数据序列的非所属指针,以及指定序列中字符数的长度。 关于序列是否以 null 结尾,没有做出任何假设。

标准库根据元素类型定义多个专用化:

  • string_view
  • wstring_view
  • u16string_view
  • u32string_view

basic_string_view 描述读取字符串数据所需的最小通用接口。 它提供对基础数据的常量访问;不会进行复制(除了 copy 函数)。 数据在任何位置都可能包含或不包含 null 值 (\0)。 basic_string_view 无法控制对象的生存期。 调用方有责任确保基础字符串数据的有效性。

接受 string_view 类型参数的函数可用于处理任何类字符串类型,而无需将函数设置为模板,或将函数限制为字符串类型的特定子集。 唯一的要求是存在从字符串类型到 string_view 的隐式转换。 所有标准字符串类型都隐式转换为 string_view,后者包含相同的元素类型。 换句话说,std::string 可转换为 string_view,但不能转换为 wstring_view

下面的示例演示采用 wstring_view 类型参数的非模板函数 f。 可以使用 std::wstringwchar_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);

语法

template <class CharType, class Traits = char_traits<CharType>>
class basic_string_view;

参数

CharType
存储在 basic_string_view 中的字符类型。 C++ 标准库提供以下 typedef,用于实现此模板的专用化。

Traits
默认为 char_traits<CharType>。

构造函数

构造函数 说明
basic_string_view 构造一个为空的 basic_string_view,或者指向所有或部分其他字符串对象数据,或指向 C 样式字符数组。

Typedef

类型名称 说明
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;

成员运算符

运算符 说明
operator= basic_string_view 或可转换字符串对象分配给另一个 basic_string_view
operator[] 返回指定索引处的元素。

成员函数

成员函数 说明
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_withC++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 返回确定反向 basic_string_view 中第一个元素位置的 const 迭代器。
remove_prefix 将指针向前移动指定数量的元素。
remove_suffix 按照从后面开始的指定元素数来减小视图大小。
rend 返回指向反向 basic_string_view 中最后一个元素下一位置的 const 迭代器。
rfind 反向搜索 basic_string_view,获取与指定字符序列匹配的第一个子字符串。
size 返回当前元素数目。
starts_withC++20 检查字符串视图是否以给定前缀开头。
substr 返回从指定索引开始的指定长度的子字符串。
swap 交换两个 basic_string_view 对象的内容。

备注

如果要求函数生成的序列长于 max_size 元素,这个函数将通过引发 length_error 类型的对象来报告长度错误。

要求

std:c++17 或更高版本。

标头<string_view>

命名空间:std

basic_string_view::at

返回对指定从 0 开始的索引处的字符的 const_reference

constexpr const_reference at(size_type offset) const;

参数

offset
要引用的元素的索引。

返回值

对由参数索引指定的位置的字符的 const_reference

注解

第一个元素的索引为零,其后续元素以正整数进行连续地索引,因此,长度为 nbasic_string_view 具有由数字 n - 1 索引的第 *n* 个元素。 对于无效索引,at 会引发异常,这与 operator[] 不同。

一般情况下,我们建议对 std::vector 这样的序列使用 at,永远不要使用 basic_string_view。 传递给序列的无效索引是一个逻辑错误,应在开发期间发现和修复。 如果某个程序不确定其索引是否有效,则应测试它们,而不是调用 at() 并依赖异常来防范草率编程。

有关详细信息,请参阅 basic_string_view::operator[]

示例

// 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;

返回值

basic_string_view 中最后一个元素的 const_reference

注解

如果 basic_string_view 为空,则会引发异常。

请记住,在修改 basic_string_view 后(例如通过调用 remove_suffix),此函数返回的元素将不再是基础数据中的最后一个元素。

示例

使用 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);

参数

str
指向字符值的指针。

len
要包含在视图中的字符数。

备注

具有 charT* 参数的构造函数假定输入以 null 结尾,但终止 null 不包含在 basic_string_view 中。

还可以使用文本构造 basic_string_view。 请参阅 operator"" sv

basic_string_view::begin

cbegin 相同。

constexpr const_iterator begin() const noexcept;

返回值

返回确定第一个元素位置的 const_iterator

basic_string_view::cbegin

返回确定范围中第一个元素位置的 const_iterator

constexpr const_iterator cbegin() const noexcept;

返回值

const 随机访问迭代器,指向范围的第一个元素,或刚超出空范围末尾的位置(对于空范围,cbegin() == cend())。

basic_string_view::cend

返回确定刚超出范围中最后一个元素的位置的 const_iterator

constexpr const_iterator cend() const noexcept;

返回值

指向刚超出范围末尾的位置的 const 随机访问迭代器。

备注

不应对 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;

参数

strv
要与此 basic_string_view 进行比较的 basic_string_view

pos
开始进行比较的 basic_string_view 的索引。

num
要比较的 basic_string_view 的最大字符数。

num2
要比较的 strv 的最大字符数。

offset
开始进行比较的 strv 的索引。

ptr
要与此 basic_string_view 进行比较的 C 字符串。

返回值

  • 如果此 basic_string_view 小于 strvptr,则为负值
  • 如果两个字符序列相等,则为零
  • 如果此 basic_string_view 大于 strvptr,则为正值

备注

compare 成员函数对每个字符序列的所有或部分执行区分大小写的比较。

示例

// 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;

参数

ptr
要复制的元素的目标字符数组。

count
要从源 basic_string_view 复制的最大字符数。

offset
要进行复制的源 basic_string_view 中的开始位置。

返回值

复制的字符数。

注解

空字符不追加到副本的末尾。

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;

参数

dest
要复制的元素的目标字符数组。

dest_size
dest 的大小。

count 要从源字符串复制的字符的最大数目。

_Off
要进行复制的源字符串中的开始位置。

返回值

复制的字符数。

备注

空字符不追加到副本的末尾。

有关详细信息,请参阅 c-runtime-library/security-features-in-the-crt

basic_string_view::crbegin

返回确定反向 basic_string_view 中第一个元素位置的 const_reverse_iterator

constexpr const_reverse_iterator crbegin() const noexcept;

返回值

确定反向 basic_string_view 中第一个元素位置的 const_reverse_iterator

basic_string_view::crend

rend 相同。

constexpr const_reverse_iterator crend() const noexcept;

返回值

返回确定反向 basic_string_view 末尾下一位置的 const_reverse_iterator

basic_string_view::data

返回原始非所属指针,它指向用于构造 basic_string_view 的对象的常量字符序列。

constexpr value_type *data() const noexcept;

返回值

指向字符序列中第一个元素的指向常量的指针。

备注

该指针无法修改字符。

basic_string_view 字符序列不一定以 null 结尾。 data 的返回类型不是有效的 C 字符串,因为未追加空字符。 空字符 \0basic_string_view 类型的对象中没有特殊含义,可以像其他字符一样成为 basic_string_view 对象的一部分。

basic_string_view::empty

测试 basic_string_view 是否包含字符。

constexpr bool empty() const noexcept;

返回值

如果 basic_string_view 对象不包含字符,则为 true;如果它至少包含一个字符,则为 false

备注

成员函数等效于 size() == 0。

basic_string_view::end

返回指向最后一个元素下一位置的随机访问 const_iterator

constexpr const_iterator end() const noexcept;

返回值

返回指向最后一个元素下一位置的随机访问 const_iterator

备注

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;

参数

c
要查找的单个字符后缀。

sv
包含要查找的后缀的字符串视图。
可以传递 std::basic_string,它会转换为 basic_string_view

x
包含要查找的后缀的以 null 结尾的字符字符串。

返回值

如果字符串视图以指定后缀结尾,则为 true;否则为 false

备注

ends_with() 是 C++20 中的新增功能。 若要使用它,请指定 /std:c++20 或更高版本编译器选项。

查看 starts_with 以检查字符串视图是否以指定前缀开头。

示例

// Requires /std:c++20 or /std:c++latest
#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;

参数

str
成员函数要搜索的 basic_string_view

chVal
成员函数要搜索的字符值。

offset
在此处开始搜索的索引。

ptr
成员函数要搜索的 C 字符串。

count
ptr 中的字符数,从第一个字符开始向前计数。

返回值

搜索成功时,则为搜索的子字符串的首个字符的索引;否则为 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;

参数

str
成员函数要搜索的 basic_string_view

chVal
成员函数要搜索的字符值。

offset
在此处开始搜索的索引。

ptr
成员函数要搜索的 C 字符串。

count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。

返回值

搜索成功时,则为搜索的子字符串的首个字符的索引;否则为 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;

参数

chVal
成员函数要搜索的字符值。

offset
在此处开始搜索的索引。

ptr
成员函数要搜索的 C 字符串。

count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。

str
成员函数要搜索的 basic_string_view

返回值

搜索成功时,则为搜索的子字符串的首个字符的索引;否则为 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;

参数

str
成员函数要搜索的 basic_string_view

chVal
成员函数要搜索的字符值。

offset
在此处结束搜索的索引。

ptr
成员函数要搜索的 C 字符串。

count
ptr 中的字符数,从第一个字符开始向前计数。

返回值

搜索成功时,则为搜索的子字符串的首个字符的索引;否则为 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;

参数

str
成员函数要搜索的 basic_string_view

chVal
成员函数要搜索的字符值。

offset
在此处结束搜索的索引。

ptr
成员函数要搜索的 C 字符串。

count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。

返回值

搜索成功时,则为搜索的子字符串的最后一个字符的索引;否则为 npos

basic_string_view::front

返回对第一个元素的 const_reference

constexpr const_reference front() const;

返回值

第一个元素的 const_reference

备注

如果 basic_string_view 为空,则会引发异常。

basic_string_view::length

返回当前元素数目。

constexpr size_type length() const noexcept;

备注

成员函数与 size 相同。

basic_string_view::max_size

返回 basic_string_view 可包含的最大字符数。

constexpr size_type max_size() const noexcept;

返回值

返回 basic_string_view 可包含的最大字符数。

注解

当运算生成长度大于 max_size()basic_string_view 时,将引发 length_error 类型异常。

basic_string_view::operator=

basic_string_view 或可转换字符串对象分配给另一个 basic_string_view

constexpr basic_string_view& operator=(const basic_string_view&) noexcept = default;

示例

   string_view s = "Hello";
   string_view s2 = s;

basic_string_view::operator[]

使用指定索引提供对字符的 const_reference

constexpr const_reference operator[](size_type offset) const;

参数

offset
要引用的元素的索引。

返回值

对由参数索引指定的位置的字符的 const_reference

注解

第一个元素的索引为零,其后续元素以正整数进行连续索引,因此,长度为 nbasic_string_view 具有由数字 n-1 索引的第 *n* 个元素。

在提供对 basic_string_view 元素的读取访问权限时,operator[] 比成员函数 at 更快。

operator[] 不检查作为参数传递的索引是否有效。 传递给 operator[] 无效的索引会导致未定义的行为。

如果基础字符串数据被所属对象修改或删除,则返回的引用可能会失效。

_ITERATOR_DEBUG_LEVEL 设置为 1 或 2 的情况下进行编译时,如果尝试访问 basic_string_view 边界以外的元素,将发生运行时错误。 有关更多信息,请参阅经过检查的迭代器

basic_string_view::rbegin

返回指向反向 basic_string_view 中第一个元素的 const 迭代器。

constexpr const_reverse_iterator rbegin() const noexcept;

返回值

返回指向反向 basic_string_view 中第一个元素的随机访问迭代器,用于确定相应非反向 basic_string_view 中最后一个元素的位置。

注解

rbegin 用于反向 basic_string_view,正如 begin 用于 basic_string_view 一样。 rbegin 可用于向后初始化迭代。

basic_string_view::remove_prefix

将指针向前移动指定数量的元素。

constexpr void remove_prefix(size_type n);

备注

使基础数据保持不变。 将 basic_string_view 指针向前移动 n 个元素,并将专用 size 数据成员设置为 size - n

basic_string_view::remove_suffix

按照从后面开始的指定元素数来减小视图大小。

constexpr void remove_suffix(size_type n);

备注

使基础数据和指向它的指针保持不变。 将专用 size 数据成员设置为 size - n

basic_string_view::rend

返回指向反向 basic_string_view 中最后一个元素下一位置的 const 迭代器。

constexpr reverse_iterator rend() const noexcept;

返回值

指向反向 basic_string_view 中最后一个元素下一位置的 const 反向随机访问迭代器。

备注

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;

参数

chVal
成员函数要搜索的字符值。

offset
在此处开始搜索的索引。

ptr
成员函数要搜索的 C 字符串。

count
在成员函数要搜索的 C 字符串中从第一个字符开始计数的字符数。

str
成员函数要搜索的 basic_string_view

返回值

搜索成功时,则为子字符串的首个字符的索引;否则为 npos

basic_string_view::size

返回 basic_string_view 中的元素数量。

constexpr size_type size() const noexcept;

返回值

basic_string_view 的长度。

备注

basic_string_view 可以修改其长度,例如通过 remove_prefixremove_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;

参数

c
要查找的单个字符前缀。

sv
包含要查找的前缀的字符串视图。
可以传递 std::basic_string,其将转换为字符串视图。

x
包含要查找的前缀的以 null 结尾的字符字符串。

返回值

如果字符串以指定的前缀开始,则为 true;否则为 false

备注

starts_with() 是 C++20 中的新增功能。 若要使用它,请指定 std:c++20 或更高版本编译器选项。

请参见 ends_with 以查看字符串是否以后缀结尾。

示例

// Requires /std:c++20 or /std:c++latest
#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;

参数

offset
从进行复制所在的位置查找元素的索引,默认值为 0。

count
子字符串中要包含的字符数(如果存在)。

返回值

一个 basic_string_view 对象,表示元素的指定子序列。

basic_string_view::swap

交换两个 basic_string_view,也就是指向基础字符串数据的指针和大小值。

constexpr void swap(basic_string_view& sv) noexcept;

参数

sv
要与目标 basic_string_view 值交换其指针和大小值的源 basic_string_view

另请参阅

<string_view>
C++ 标准库中的线程安全