共用方式為


basic_string_view 類別

類別範本 basic_string_view<charT> 已在 C++17 中新增,以做為函式接受各種不相關的字串類型的安全且有效率的方式,而不需要在這些類型上範本化函式。 類別會保存連續字元資料序列的非擁有指標,以及指定序列中字元數目的長度。 不會假設序列是否為 Null 終止。

標準程式庫會根據元素的類型定義數個特製化:

  • string_view
  • wstring_view
  • u16string_view
  • u32string_view

basic_string_view描述讀取字串資料所需的最小通用介面。 它提供基礎資料的 const 存取權;它不會複製任何複本(函式 copy 除外)。 資料可能或可能不會在任何位置包含 Null 值 ( \0 )。 basic_string_view無法控制物件的存留期。 呼叫端有責任確保基礎字串資料有效。

可接受 型 string_view 別參數的函式,可用來處理任何類似字串的類型,而不需要將函式變成範本,或將函式限制為字串類型的特定子集。 唯一的需求是從字串類型隱含轉換到 string_view 。 所有標準字串類型都會隱含轉換成 string_view 包含相同專案型別的 。 換句話說, std::string 可以轉換成 , string_view 但無法轉換為 wstring_view

下列範例顯示採用 類型 wstring_view 參數的非樣板函 f 式。 您可以使用 、 和 winrt::hstring 類型的 std::wstringwchar_t* 引數來呼叫它。

// 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++ 標準程式庫提供下列 typedefs 來特製化此範本。

Traits
預設為 char_traits<CharType>。

建構函式

建構函式 描述
basic_string_view basic_string_view建構空的 ,否則會指向其他字串物件資料的所有或部分,或指向 C 樣式字元陣列。

Typedefs

類型名稱 描述
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 s 是不可變的。
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 const傳回反覆運算器,定址反轉 basic_string_view 中的第一個專案。
remove_prefix 將指標往前移動指定的專案數目。
remove_suffix 從後面開始的指定專案數目來減少檢視的大小。
rend const傳回反覆運算器,指向反轉 basic_string_view 中的最後一個專案之後的反覆運算器。
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

const_reference 回至指定之以零起始之索引處的 字元。

constexpr const_reference at(size_type offset) const;

參數

offset
要參考之專案的索引。

傳回值

const_reference位於參數索引所指定位置之字元的 。

備註

第一個元素的索引為零,而下列元素會依正整數連續編制索引,因此 basic_string_view 長度 n 的 具有以數位 n - 1 為 索引的 * n *th 元素。 at 會擲回無效索引的例外狀況,不同于 operator[]

一般而言,我們建議 at 針對 和 之類的 std::vectorbasic_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;

傳回值

const_reference中最後一個專案的 basic_string_view

備註

如果 是空的, basic_string_view 則擲回例外狀況。

請記住,在 修改 之後 basic_string_view ,例如呼叫 remove_suffix ,則此函式傳回的專案不再是基礎資料中的最後一個專案。

範例

string_view使用 C 字串常值建構的 ,不包含終止 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 字串。

傳回值

  • 如果此值小於 strv 或 ,則為 basic_string_view 負值ptr
  • 如果兩個字元序列相等,則為零
  • 如果此值大於 strv 或,則為 basic_string_view 正值ptr

備註

成員 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 起始位置。

傳回值

已複製的字元數。

備註

Null 字元不會附加至複本結尾。

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
來源字串中要建立複本的開始位置。

傳回值

已複製的字元數。

備註

Null 字元不會附加至複本結尾。

如需詳細資訊,請參閱 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;

傳回值

const_reverse_iterator,定址反轉 basic_string_view 中的第一個專案。

basic_string_view::crend

rend 相同。

constexpr const_reverse_iterator crend() const noexcept;

傳回值

const_reverse_iterator 回 ,其定址位址超過反轉 basic_string_view 的結尾。

basic_string_view::data

傳回用來建構 basic_string_view 之物件之 const 字元序列的原始非擁有指標。

constexpr value_type *data() const noexcept;

傳回值

字元序列第一個專案的指標對 const。

備註

指標無法修改字元。

字元序列 basic_string_view 不一定以 Null 結尾。 的傳回型 data 別不是有效的 C 字串,因為不會附加 Null 字元。 Null 字元 \0 在 型 basic_string_view 別的物件中沒有特殊意義,而且可能是物件的一部分 basic_string_view ,就像任何其他字元一樣。

basic_string_view::empty

測試 是否 basic_string_view 包含字元。

constexpr bool empty() const noexcept;

傳回值

truebasic_string_view如果物件不包含任何字元,則為 , 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 數。

備註

當作業產生 basic_string_view 長度大於 max_size() 的 時,會擲回 型 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位於參數索引所指定位置之字元的 。

備註

第一個元素的索引為零,而下列元素會依正整數連續編制索引,因此 basic_string_view 長度 n 的 a 具有以數位 n-1 為 索引的 * n *th 元素。

operator[] 比成員函 at 式更快,可提供 專案的 basic_string_view 讀取權限。

operator[] 不會檢查傳遞為引數的索引是否有效。 傳遞至 operator[] 的索引無效,會導致未定義的行為。

如果擁有物件修改或刪除基礎字串資料,傳回的參考可能會失效。

使用 設定為 1 或 2 進行 _ITERATOR_DEBUG_LEVEL 編譯時,如果您嘗試存取 超出 界限的專案 basic_string_view ,就會發生執行階段錯誤。 如需詳細資訊,請參閱 Checked Iterators

basic_string_view::rbegin

const 反覆運算器傳回反轉 basic_string_view 的第一個專案。

constexpr const_reverse_iterator rbegin() const noexcept;

傳回值

將隨機存取反覆運算器傳回反轉 basic_string_view 的第一個專案,定址對應未反 basic_string_view 轉中的最後一個專案。

備註

rbegin與 反轉 basic_string_view 搭配使用,就像 搭配 使用一 basic_string_viewbeginrbegin 可用來向後初始化反復專案。

basic_string_view::remove_prefix

將指標往前移動指定的專案數目。

constexpr void remove_prefix(size_type n);

備註

讓基礎資料保持不變。 將 basic_string_viewn 指標往前移動,並將私 size 用資料成員設定為 size - n

basic_string_view::remove_suffix

從後面開始的指定專案數目來減少檢視的大小。

constexpr void remove_suffix(size_type n);

備註

讓基礎資料及其指標保持不變。 將私用 size 資料成員設定為 size - n

basic_string_view::rend

const傳回反覆運算器,指向反轉 basic_string_view 中的最後一個專案之後的反覆運算器。

constexpr reverse_iterator rend() const noexcept;

傳回值

反向 const 隨機存取反覆運算器,指向反轉 basic_string_view 中最後一個專案的過去。

備註

rend與 反轉 basic_string_view 搭配使用,就像 搭配 使用一 basic_string_viewendrend 可用來測試反向反覆運算器是否已到達其 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可以修改其長度,例如 by 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++ 標準程式庫中的執行緒安全