std::basic_string_view::at(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸŽ€ 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_range if pos >= 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 of std::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