std::experimental::basic_string_view<CharT,Traits>::operator[]

constexpr const_reference operator[](size_type pos) const;
(库基础 TS)

返回到指定位置 pos 的字符的 const 引用。

不进行边界检查:若 pos >= size() 则其行为未定义。

参数

pos - 要返回字符的位置

返回值

到所请求字符的 const 引用

异常

不抛出

复杂度

常数。

注解

std::basic_string::operator[] 不同,basic_string_view::operator[](size()) 有未定义行为而非返回 CharT()

示例

#include <iostream>
#include <experimental/string_view>
int main()
{
    std::string str = "Exemplar";
    std::experimental::string_view v = str;
    std::cout << v[2] << '\n';
//  v[2] = 'y'; // 错误: 不能通过字符串视图进行修改
    str[2] = 'y';
    std::cout << v[2] << '\n';
}

输出:

e
y

参阅

带边界检查访问指定字符
(公开成员函数)