std::basic_string<CharT,Traits,Allocator>::begin, std::basic_string<CharT,Traits,Allocator>::cbegin
来自cppreference.com
                    
                                        
                    < cpp | string | basic string
                    
                                                            
                    | (1) | ||
|   iterator begin();  | 
(C++11 前) | |
|   iterator begin() noexcept;  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr iterator begin() noexcept;  | 
(C++20 起) | |
| (2) | ||
|   const_iterator begin() const;  | 
(C++11 前) | |
|   const_iterator begin() const noexcept;  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr const_iterator begin() const noexcept;  | 
(C++20 起) | |
| (3) | ||
|   const_iterator cbegin() const;  | 
(C++11 前) | |
|   const_iterator cbegin() const noexcept;  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr const_iterator cbegin() const noexcept;  | 
(C++20 起) | |
返回指向字符串首字符的迭代器。
begin() 返回可变或常迭代器,取决于 *this 的常量性。
cbegin() 始终返回常迭代器。它等价于 const_cast<const basic_string&>(*this).begin() 。
参数
(无)
返回值
指向首字符的迭代器。
复杂度
常数。
注解
在 libstdc++ 中,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'; // 错误: i 是常迭代器 }
输出:
exemplar e
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 1192 | C++98 |  std::string 没有成员函数 cbegin()
 | 
已添加 | 
参阅
|   返回指向末尾的迭代器  (公开成员函数)  | |
|    返回指向起始位置的迭代器  ( std::basic_string_view<CharT,Traits> 的公开成员函数)  |