std::experi...:operator[](3)

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

✨ NAME ✨

okay so this is the std::experi...:operator[](3) page β€” literally the std::experimental::basic_string_view::operator[] page from the C++ Standard Libary manual. yes bestie, β€œLibary” is how it’s spelled up there, and I’m not gonna gaslight you about it πŸ’…

it’s giving β€œaccess one character by index” energy, and the full name is std::experimental::basic_string_view::operator[] β€” iconic, no cap πŸŽ€


🌸 Synopsis 🌸

here’s the signature, bestie:

constexpr const_reference operator[](size_type pos)        (library
fundamentals TS)
const;

Returns a const reference to the character at specified location pos.

and listen β€” there is no bounds checking performed: the behavior is undefined if pos >= size(). that’s not a vibe, that’s UB, so do not play with it 😭πŸ”₯

πŸ¦‹ Parameters πŸ¦‹

  • pos β€” position of the character to return

πŸ’œ Return value πŸ’œ

Const reference to the requested character. slay ✨

🌈 Exceptions 🌈

Does not throw. literally never throws, periodt πŸ’…

⭐ Complexity ⭐

Constant.

πŸŽ€ Notes πŸŽ€

Unlike std::basic_string::operator[], basic_string_view::operator[](size()) has undefined behavior instead of returning CharT(). this is the messy part, bestie β€” do not confuse the two 🫢

πŸ”₯ Example πŸ”₯

run this code:

// Run this code

#include <iostream>
#include <experimental/string_view>
int main()
{
    std::string str = "Exemplar";
    std::experimental::string_view v = str;
    std::cout << v[2] << '\n';
//  v[2] = 'y'; // Error: cannot modify through a string view
    str[2] = 'y';
    std::cout << v[2] << '\n';
}

πŸ“ Output πŸ“

e
y

πŸ‘‘ See also πŸ‘‘

  • at β€” access specified character with bounds checking (public member function)

🫢 Category 🫢

  • * Noindexed pages

mwah πŸ’‹ β€” source: http://cppreference.com, dated 2024.06.10. that’s the whole manual-page tea, bestie 🌟

πŸ“¦ where this page comes from