C++ 参考手册

位置:首页 > C++ 参考手册 >字符串库 >std::basic_string_view > std::basic_string_view<CharT,Traits>::begin, std::basic_string_view<CharT,Traits>::cbegin

constexpr const_iterator begin() const noexcept;
(C++17 起)
constexpr const_iterator cbegin() const noexcept;
(C++17 起)

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

range-begin-end.svg

参数

(无)

返回值

指向首字符的 const_iterator

复杂度

常数

示例

#include <iostream>
#include <string_view>
 
int main()
{
    std::string_view str_view("abcd");
 
    auto begin = str_view.begin();
    auto cbegin = str_view.cbegin();
 
    std::cout << *begin << '\n';
    std::cout << *cbegin << '\n';
 
    std::cout << std::boolalpha << (begin == cbegin) << '\n';
    std::cout << std::boolalpha << (*begin == *cbegin) << '\n';
}

输出:

a
a
true
true

参阅

返回指向结尾的迭代器
(公开成员函数)