std::basic_string<CharT,Traits,Allocator>::subview
Von de.cppreference.net
constexpr std::basic_string_view<CharT, Traits> subview( size_type pos = 0,
size_type count = npos ) const;
|
(seit C++26) | |
Gibt eine Ansicht des Teilstrings [pos, pos + rlen) zurück, wobei rlen das kleinere von count und size() - pos ist.
Entspricht return std::basic_string_view<CharT, Traits>(*this).subview(pos, count);.
Parameter
| pos | - | Position des ersten einzubeziehenden Zeichens |
| count | - | Länge der Unteransicht |
Rückgabewert
Ansicht des Teilstrings [pos, pos + rlen).
Ausnahmen
std::out_of_range falls pos > size().
Komplexität
Konstant.
Hinweise
| Feature-Test-Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_string_subview |
202506L |
(C++26) | std::basic_string::subview, std::basic_string_view::subview
|
Beispiel
Diesen Code ausführen
#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';
}
}
Mögliche Ausgabe:
Exception: basic_string_view::substr: __pos (which is 26) > __size (which is 13)
Siehe auch
| gibt einen Teilstring zurück (öffentliche Elementfunktion) | |
(C++26) |
gibt eine Unteransicht zurück (öffentliche Elementfunktion von std::basic_string_view<CharT,Traits>)
|