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

出自cppreference.com
 
 
 
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() 返回可變常量迭代器,取決於 *this 的常量性。

cbegin() 始終返回常量迭代器。它等價於 const_cast<const basic_string&>(*this).begin()

參數

(無)

返回值

指向首字符的迭代器

複雜度

常數

註解

libc++ 將 cbegin() 向後移植到 C++98 模式。

示例

#include <string>
#include <iostream>

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

    auto i = s.cbegin();
    std::cout << *i << '\n';
//  *i = 'E'; // 错误:i 是常量迭代器
}

輸出:

exemplar
e

參閱

(C++11)
返回指向末尾的迭代器
(公開成員函數) [編輯]
返回指向起始位置的迭代器
(std::basic_string_view<CharT,Traits> 的公開成員函數) [編輯]