Namespaces
Variants

std::basic_string_view<CharT,Traits>::subview

ja.cppreference.net より
 
 
 
 
constexpr std::basic_string_view<CharT, Traits> subview( size_type pos = 0,
                                                         size_type count = npos ) const;
(C++26以上)

部分文字列のビューを返す [pospos + rlen)。ここで rlencountsize() - pos の小さい方です。

引数

pos - 最初の文字の位置
count - 要求する長さ

戻り値

部分文字列のビュー [pospos + rlen)

例外

std::out_of_rangepos > size() の場合)。

計算量

定数時間。

注記

機能テストマクロ Std 機能
__cpp_lib_string_subview 202506L (C++26) std::basic_string_view::subview, std::basic_string::subview

#include <cassert>
#include <iostream>
#include <string_view>

int main()
{
    const std::string_view s{"Life is life!"};
    assert(s.subview(5) == "is life!");
    assert(s.subview(5, 13) == "is life!");
    assert(s.subview(5, 2) == "is");

    try
    {
        // pos is out of bounds, throws
        const auto pos{s.length() + 13};
        [[maybe_unused]] auto x_x{s.subview(pos)};
    }
    catch (const std::out_of_range& ex)
    {
        std::cout << "Exception: " << ex.what() << '\n';
    }
}

出力例:

Exception: basic_string_view::substr: __pos (which is 26) > __size (which is 13)

関連項目

部分文字列を返す
(パブリックメンバ関数)
(C++26)
部分ビューを返す
(パブリックメンバ関数)