std::basic_string<CharT,Traits,Allocator>::begin, std::basic_string<CharT,Traits,Allocator>::cbegin
來自 cppreference.com
< cpp | string | basic_string
iterator begin(); |
(1) | (C++11 起無異常丟擲) (C++20 起為 constexpr) |
const_iterator begin() const; |
(2) | (C++11 起無異常丟擲) (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 <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> 的公共成員函式) |