std::basic_...:operator[](3)
okay so welcome to std::basic_...:operator[](3) from the C++ Standard Libary manual β this is the page for std::basic_string_view::operator[], and bestie, it's giving βtiny function, big main-character energyβ β¨
πΈ NAME πΈ
std::basic_string_view::operator[] β yep, literally std::basic_string_view::operator[] π
π¦ Synopsis π¦
constexpr const_reference operator[]( size_type pos ) const; (since C++17)
Returns a const reference to the character at the specified location pos. Since C++17, no cap π«Ά
β οΈ No bounds checking is performed: the behavior is undefined if pos >= size(). Like, not βoopsieβ undefined β actual undefined behavior. Do not play with her π
π Parameters π
posβ position of the character to return
π Return value π
Const reference to the requested character. Periodt.
π Exceptions π
Does not throw. Sheβs chill like that.
β‘ Complexity β‘
Constant.
πΈ Notes πΈ
Unlike std::basic_string::operator[], std::basic_string_view::operator[](size()) has undefined behavior instead of returning CharT(). This is the messy little difference, iykyk π
π Example π
// Run this code
#include <iostream>
#include <string_view>
int main()
{
std::string str = "Exemplar";
std::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β accesses the specified character with bounds checking (public member function)operator[]β accesses the specified character (public member function ofstd::basic_string<CharT,Traits,Allocator>)
mwah π this iconic little page came from http://cppreference.com, dated 2024.06.10 β catch you in the next manpage, 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