std::basic_string<CharT,Traits,Allocator>::begin, std::basic_string<CharT,Traits,Allocator>::cbegin

Returns an iterator to the first character of the string.

# Declarations

iterator begin();

(noexcept since C++11) (constexpr since C++20)

const_iterator begin() const;

(noexcept since C++11) (constexpr since C++20)

const_iterator cbegin() const noexcept;

(since C++11) (constexpr since C++20)

# Return value

Iterator to the first character.

# Notes

libc++ backports cbegin() to C++98 mode.

# Example

#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
}

# See also