Namespaces
Variants

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

From cppreference.net
std::basic_string
iterator begin ( ) ;
(1) (C++11以降 noexcept)
(C++20以降 constexpr)
const_iterator begin ( ) const ;
(2) (C++11以降 noexcept)
(C++20以降 constexpr)
const_iterator cbegin ( ) const noexcept ;
(3) (C++11以降)
(C++20以降 constexpr)

文字列の最初の文字へのイテレータを返します。

begin() は、 mutable または constant イテレータを返します。これは * this のconst性に依存します。

cbegin() は常に 定数イテレータ を返します。これは const_cast < const basic_string & > ( * this ) . begin ( ) と等価です。

range-begin-end.svg

目次

パラメータ

(なし)

戻り値

最初の文字へのイテレータ。

計算量

定数。

注記

libc++は cbegin() をC++98モードにバックポートします。

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

出力:

exemplar
e

関連項目

(C++11)
終端へのイテレータを返す
(公開メンバ関数)
先頭へのイテレータを返す
( std::basic_string_view<CharT,Traits> の公開メンバ関数)