std::experimental::basic_string_view<CharT,Traits>::operator[]
来自 cppreference.cn
< cpp | experimental | basic string view
constexpr const_reference operator[](size_type pos) const; |
(库基础 TS) | |
返回到指定位置 pos
处字符的常量引用。
不执行边界检查:如果 pos >= size(),则行为未定义。
目录 |
[编辑] 参数
pos | - | 要返回的字符的位置 |
[编辑] 返回值
到请求字符的常量引用
[编辑] 异常
不抛出
[编辑] 复杂度
常数。
[编辑] 注意
与 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'; // Error: cannot modify through a string view str[2] = 'y'; std::cout << v[2] << '\n'; }
输出
e y
[编辑] 参见
访问带有边界检查的指定字符 (公共成员函数) |