std::basic_string_view::at(3)
π NAME π
std::basic_string_view::at - std::basic_string_view::at β yes bestie, the NAME is literally the same thing twice, and this is the std::basic_string_view::at(3) page from the C++ Standard Libary manual. iconic behavior, no notes π
.
β¨ Synopsis β¨
okay so here's the signature, and it's giving "I know my type" energy:
constexpr const_reference at( size_type pos ) const; (since C++17)
Returns a const reference to the character at specified location pos. Bounds checking is performed β yes, she checks β and an exception of type std::out_of_range will be thrown on invalid access. no silent chaos here, periodt.
πΈ Parameters πΈ
posβ position of the character to return. that's the spot bestie π«Ά
π Return value π
- Const reference to the requested character. literally what you asked for, returned by const reference β¨
π¨ Exceptions π¨
- Throws
std::out_of_rangeifpos >= size(). that's the line, no cap π¨
β‘ Complexity β‘
- Constant. lowkey the chillest complexity tier β‘
π§ͺ Example π§ͺ
// Run this 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';
}
}
π₯οΈ Possible output π₯οΈ
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)
π See also π
operator[]β accesses the specified character (public member function).atβ accesses the specified character with bounds checking (public member function ofstd::basic_string<CharT,Traits,Allocator>).
mwah π β source http://cppreference.com, dated 2024.06.10, page std::basic_string_view::at(3). bye bestie π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms