std::basic_string_view<CharT,Traits>::operator[]
来自 cppreference.cn
< cpp | string | basic string view
constexpr const_reference operator[]( size_type pos ) const; |
(C++17 起) | |
返回对指定位置 pos 字符的常量引用。
不执行边界检查:如果 pos >= size(),则行为未定义。
目录 |
[编辑] 参数
pos | - | 要返回的字符的位置 |
[编辑] 返回值
对所请求字符的常量引用。
[编辑] 异常
不抛出异常。
[编辑] 复杂度
常数时间。
[编辑] 注释
与 std::basic_string::operator[] 不同,std::basic_string_view::operator[](size()) 具有未定义行为,而不是返回 CharT()
。
[编辑] 示例
运行此代码
#include <iostream> #include <string_view> int main() { std::string str = "Exemplar"; std::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
[编辑] 参见
访问指定字符,并进行边界检查 (public member function) | |
访问指定字符 ( std::basic_string<CharT,Traits,Allocator> 的 public 成员函数) |