std::experi...:operator[](3)
β¨ 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
- 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