std::basic_string_view<CharT,Traits>:: operator[]
|
constexpr
const_reference operator
[
]
(
size_type pos
)
const
;
|
(C++17 起) | |
返回指定位置 pos 处字符的常量引用。
|
若 pos < size ( ) 为 false ,则行为未定义。 |
(C++26 前) |
|
若 pos < size ( ) 为 false : |
(C++26 起) |
目录 |
参数
| pos | - | 要返回字符的位置 |
返回值
data_
[
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'; // 错误:无法通过 string view 修改内容 str[2] = 'y'; std::cout << v[2] << '\n'; }
输出:
e y
参见
|
访问指定字符(带边界检查)
(公开成员函数) |
|
|
访问指定字符
(
std::basic_string<CharT,Traits,Allocator>
的公开成员函数)
|