std::basic_string_view<CharT,Traits>::operator[]
出自cppreference.com
| |
(C++17 起) | |
返回到位於指定位置 pos 的字符的 const 引用。
|
如果 |
(C++26 前) |
|
如果 |
(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'; // 错误:不能通过字符串视图修改
str[2] = 'y';
std::cout << v[2] << '\n';
}
輸出:
e
y