命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
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)

返回指向字符串首字符的迭代器。

begin() 返回一个 可变常量 迭代器,取决于 *this 的常量性。

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> 的公共成员函数) [编辑]