|
|
constexpr const_reference at( size_type pos)const; | (since C++17) | |
Returns aconst
reference to the character at specified locationpos. Bounds checking is performed, exception of typestd::out_of_range will be thrown on invalid access.
Contents |
pos | - | position of the character to return |
Const
reference to the requested character.
Throwsstd::out_of_range ifpos>= size().
Constant.
#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(conststd::out_of_range& e){std::cout<<"Whooops. Index is out of range.\n";std::cout<< e.what()<<'\n';}}
Possible output:
0: a1: b2: c3: d4: e5: f6: Whooops. Index is out of range.basic_string_view::at: __pos (which is 6) >= this->size() (which is 6)
accesses the specified character (public member function)[edit] | |
accesses the specified character with bounds checking (public member function of std::basic_string<CharT,Traits,Allocator> )[edit] |