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::stringstring_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 空であるか、他の文字列オブジェクトのデータのすべてまたは一部を指すか、または C スタイルの文字配列を指す basic_string_view を構築します。

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 説明
operator= basic_string_view または変換可能な文字列オブジェクトを別の basic_string_view に割り当てます。
operator[] 指定したインデックス位置にある要素を返します。

メンバー関数

メンバー関数 説明
at 指定した位置にある要素への const_reference を返します。
back 最後の要素への const_reference を返します。
begin 最初の要素を指す const 反復子を返します。 (basic_string_view は変更不可能です。)
cbegin begin と同じ。
cend 最後の要素の 1 つ後を指す 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 内の最後の要素の 1 つ後を指す const 反復子を返します。
rfind 指定された文字シーケンスに一致する部分文字列の最初の basic_string_view の出現を逆方向に検索します。
size 要素の現在の要素数を返します。
starts_withC++20 文字列ビューが特定のプレフィックスで始まるかどうかを確認します。
substr 指定したインデックスを開始位置として、指定した長さの部分文字列を返します。
swap 2 つの basic_string_view オブジェクトの内容を交換します。

解説

関数が max_size の要素よりも長いシーケンスを生成するように指示された場合、関数は length_error 型のオブジェクトをスローして長さエラーを報告します。

必要条件

std:c++17 以降。

ヘッダー:<string_view>

名前空間std:

basic_string_view::at

指定した 0 から始まるインデックス位置にある文字に a const_reference を返します。

constexpr const_reference at(size_type offset) const;

パラメーター

offset
参照される要素のインデックス。

戻り値

パラメーターのインデックスで指定した位置の文字への const_reference

解説

最初の要素にはゼロから始まるインデックスがあり、次の要素には、正の整数によって連続したインデックスが付けられます。これにより、長さ nbasic_string_view には、n - 1 の数でインデックス付けされた *n* 番目の要素が指定されます。 at とは異なり operator[]、無効なインデックスの例外をスローします。

一般に、std::vectorbasic_string_view などのシーケンスに対して at を使用しないことをお勧めします。 シーケンスに渡された無効なインデックスは、開発中に検出および修正する必要のある論理エラーです。 インデックスが有効であることをプログラムで特定できない場合は、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

2 つのオブジェクトが等しいかどうか、または一方が辞書順で他方よりも小さいかどうかを判断するために、指定した 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_viewstrv または ptr 未満である場合は、負の値
  • 2 つの文字シーケンスが等しい場合は 0。
  • この basic_string_viewstrv または 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

の代わりに copy使用するセキュリティで保護された CRT コピー関数。

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

反転された 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 の末尾の 1 つ後ろを示す const_reverse_iterator を返します。

basic_string_view::data

basic_string_view の構築に使用されたオブジェクトの const 文字シーケンスへの、未処理の非所有ポインターを返します。

constexpr value_type *data() const noexcept;

戻り値

文字シーケンスの最初の要素への、const へのポインター。

解説

ポインターは文字を変更できません。

basic_string_view 文字のシーケンスは必ずしも null で終わるとは限りません。 null 文字が追加されないため、data の戻り値の型は有効な C 文字列ではありません。 null 文字 \0basic_string_view 型のオブジェクトでは特別な意味を持たず、他の文字と同様に basic_string_view オブジェクトの一部である可能性があります。

basic_string_view::empty

basic_string_view に文字が含まれているかどうかをテストします。

constexpr bool empty() const noexcept;

戻り値

basic_string_view オブジェクトに文字が含まれていない場合は true、1 つ以上の文字が含まれている場合は false

解説

このメンバー関数は、size() == 0 と同じです。

basic_string_view::end

最後の要素の 1 つ後を指すランダム アクセス const_iterator を返します。

constexpr const_iterator end() const noexcept;

戻り値

最後の要素の 1 つ後を指すランダム アクセス const_iterator を返します。

解説

end は、const_iteratorbasic_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* 番目の要素が指定されます。

operator[] はメンバー関数 at より高速で、basic_string_view の要素への読み取りアクセスを提供します。

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 は、beginbasic_string_view で使用されるのと同様に、反転された basic_string_view で使用されます。 rbegin を使用して、イテレーションを後方に初期化できます。

basic_string_view::remove_prefix

指定された数の要素でポインターを前方に移動します。

constexpr void remove_prefix(size_type n);

解説

基になるデータを変更せずに残します。 n 個の要素だけ basic_string_view ポインターを前方に移動し、プライベート 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 内の最後の要素の 1 つ後を指す const 反復子を返します。

constexpr reverse_iterator rend() const noexcept;

戻り値

反転 basic_string_view 内の最後の要素の 1 つ後ろを指す const 逆順ランダム アクセス反復子。

解説

rend は、endbasic_string_view で使用されるのと同様に、反転された 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

2 つの basic_string_view を交換します。つまり、基になる文字列データへのポインターとサイズ値です。

constexpr void swap(basic_string_view& sv) noexcept;

パラメーター

sv
ポインターとサイズの値を宛先の basic_string_view の値と交換するソース basic_string_view

関連項目

<string_view>
C++ 標準ライブラリ内のスレッド セーフ