std::basic_string_view<CharT,Traits>::substr
From cppreference.com
< cpp | string | basic string view
| constexpr basic_string_view substr( size_type pos = 0, size_type count = npos ) const; |
(C++17以降) | |
rlen を、count と size() - pos のうち小さい方としたときの、ビュー [pos, pos + rlen) を返します。
目次 |
[編集] パラメータ
| pos | - | 最初の文字の位置 |
| count | - | 要求された長さ |
[編集] 返り値
部分文字列のビュー [pos, pos + rlen)。
[編集] 例外
pos > size() の場合、std::out_of_range。
[編集] 複雑性
定数。
[編集] 例
このコードを実行
#include <cstddef> #include <iostream> #include <stdexcept> #include <string_view> int main() { typedef std::size_t count_t, pos_t; constexpr std::string_view data{"ABCDEF"}; std::cout << data.substr() << '\n'; // ABCDEF, i.e. data[0, 5] that is [0, 6) std::cout << data.substr(pos_t(1)) << '\n'; // BCDEF, i.e. [1, 6) std::cout << data.substr(pos_t(2), count_t(3)) << '\n'; // CDE, i.e. [2, 2 + 3) std::cout << data.substr(pos_t(4), count_t(42)) << '\n'; // EF, i.e. [4, 6) try { [[maybe_unused]] auto sub = data.substr(pos_t(666), count_t(1)); // throws: pos > size() } catch (std::out_of_range const& ex) { std::cout << ex.what() << '\n'; } }
実行結果の例
ABCDEF BCDEF CDE EF basic_string_view::substr: __pos (which is 666) > __size (which is 6)
[編集] 関連項目
| 文字をコピーする (public member function) | |
| ビュー内の文字を検索する (public member function) | |
| 部分文字列を返す (public member function of std::basic_string<CharT,Traits,Allocator>) |