std::basic_string_view<CharT,Traits>::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.
Parameter
| pos | - | Position des ersten Zeichens |
| count | - | angeforderte Länge |
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_view::subview, std::basic_string::subview
|
Beispiel
Diesen Code ausführen
#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';
}
}
Mögliche Ausgabe:
Exception: basic_string_view::substr: __pos (which is 26) > __size (which is 13)
Siehe auch
| gibt einen Teilstring zurück (öffentliche Mitgliedsfunktion) | |
(C++26) |
gibt eine Unteransicht zurück (öffentliche Mitgliedsfunktion) |