std::basic_string::begin,(3)

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

πŸ’– std::basic_string::begin,(3) πŸ’–

okay so bestie, we're reading the C++ Standard Libary manual page for std::basic_string::begin,(3) β€” yes, that exact title, comma and all, she's quirky like that. This is the group chat where begin() and cbegin() get explained, and honestly? iconic. 🫢

🌸 NAME 🌸

std::basic_string::begin, β€” and the original NAME line literally has std::basic_string::begin, - std::basic_string::begin,, so we're keeping both appearances because details are sacred. periodt. πŸ’…

✨ Synopsis ✨

iterator begin();                       (1) (noexcept since C++11)
                                            (constexpr since C++20)
const_iterator begin() const;           (2) (noexcept since C++11)
                                            (constexpr since C++20)
const_iterator cbegin() const noexcept; (3) (since C++11)
                                            (constexpr since C++20)

It returns an iterator to the first character of the string. main character energy, genuinely. 🌟

begin() returns a mutable or constant iterator, depending on the constness of *this. So the constness of *this is doing the vibe check here. 😌

cbegin() always returns a constant iterator. It is equivalent to const_cast<const basic_string&>(*this).begin() β€” yes, that's the exact equivalence, no cap. πŸͺ„

There's also a diagram situation named range-begin-end.svg, so picture the range begin/end visual there, bestie. πŸ¦‹

πŸŽ€ Parameters πŸŽ€

None. Zero. (none). Not a single argument. πŸ’–

⭐ Return value ⭐

Iterator to the first character. That's the whole return, and she ate. πŸ‘‘

πŸ”₯ Complexity πŸ”₯

Constant.

πŸ’œ Notes πŸ’œ

libc++ backports cbegin() to C++98 mode. cute of her, fr fr. 🌈

πŸ“ Example πŸ“

// Run this code

#include <iostream>
#include <string>

int main()
{
    std::string s("Exemplar");
    *s.begin() = 'e';
    std::cout << s << '\n';

    auto i = s.cbegin();
    std::cout << *i << '\n';
//  *i = 'E'; // error: i is a constant iterator
}

🌸 Output 🌸

exemplar
e

🧚 See also 🧚

  • end β€” returns an iterator to the end
  • cend β€” (public member function) (C++11)
  • begin β€” returns an iterator to the beginning
  • cbegin β€” (public member function of std::basic_string_view<CharT,Traits>)

source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::basic_string::begin,(3) β€” okay that's the whole manual page, bestie, go forth and iterator πŸ’…βœ¨πŸ’–

πŸ“¦ where this page comes from