std::basic_string<CharT,Traits,Allocator>::subview
ja.cppreference.net から
constexpr std::basic_string_view<CharT, Traits> subview( size_type pos = 0,
size_type count = npos ) const;
|
(C++26 から) | |
部分文字列 [pos, pos + rlen) のビューを返します。ここで rlen は count と size() - pos の小さい方です。
return std::basic_string_view<CharT, Traits>(*this).subview(pos, count); と同等です。
引数
| pos | - | 含める最初の文字の位置 |
| count | - | サブビューの長さ |
戻り値
部分文字列 [pos, pos + rlen) のビュー。
例外
の場合、std::out_of_range。
pos > size()
計算量
定数時間。
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_string_subview |
202506L |
(C++26) | std::basic_string::subview, std::basic_string_view::subview
|
例
このコードを実行
#include <cassert>
#include <iostream>
#include <string>
#include <string_view>
int main()
{
const std::string 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) |
サブビューを返す ( std::basic_string_view<CharT,Traits>のパブリックメンバ関数)
|