std::basic_string_view<CharT,Traits>::at
Depuis fr.cppreference.net
constexpr const_reference at( size_type pos ) const;
|
(depuis C++17) | |
Renvoie une const référence au caractère à l'emplacement spécifié pos. Une vérification des limites est effectuée. En cas d'accès invalide, une exception de type std::out_of_range sera levée.
Paramètres
| pos | - | position du caractère à retourner |
Valeur de retour
Const
référence au caractère demandé.
Exceptions
Lance std::out_of_range si pos >= size ( ) .
Complexité
Constante.
Exemple
Exécuter ce code
#include <iostream> #include <stdexcept> #include <string_view> int main() { std::string_view str_view("abcdef"); try { for (std::size_t i = 0; true; ++i) std::cout << i << ": " << str_view.at(i) << '\n'; } catch (const std::out_of_range& e) { std::cout << "Whooops. Index is out of range.\n"; std::cout << e.what() << '\n'; } }
Sortie possible :
0: a 1: b 2: c 3: d 4: e 5: f 6: Whooops. Index is out of range. basic_string_view::at: __pos (which is 6) >= this->size() (which is 6)
Voir aussi
|
accède au caractère spécifié
(fonction membre publique) |
|
|
accède au caractère spécifié avec vérification des limites
(fonction membre publique de
std::basic_string<CharT,Traits,Allocator>
)
|