首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_string_view::operator[]

constexpr const_reference operator const;

(since C++17)

返回对指定位置的字符的Const引用。pos...

不执行边界检查:如果pos >= size()...

参数

pos

-

position of the character to return

返回值

引用请求的字符。

例外

不会扔。

复杂性

常量。

注记

不像std::basic_string::operator[],,,std::basic_string_view::operator[](size())具有未定义的行为,而不是返回CharT()...

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
e
y

二次

另见

at

access specified character with bounds checking (public member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券