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

std::basic_string::operator[]

reference operator;

(1)

const_reference operator const;

(2)

返回对指定位置的字符的引用。pos.不执行边界检查。

1) If pos == size(), the behavior is undefined. 2) If pos == size(), a reference to the character with value CharT() (the null character) is returned.

(until C++11)

If pos == size(), a reference to the character with value CharT() (the null character) is returned. For the first (non-const) version, the behavior is undefined if this character is modified to any value other than charT().

(since C++11)

参数

pos

-

position of the character to return

返回值

引用请求的字符。

复杂性

常量。

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
 
int main()
{
    std::string const e("Exemplar");
    for (unsigned i = e.length() - 1; i != 0; i /= 2)
        std::cout << e[i];
    std::cout << '\n';
 
    const char* c = &e[0];
    std::cout << c << '\n'; // print as a C string
 
    //Change the last character of s into a 'y'
    std::string s("Exemplar ");
    s[s.size()-1] = 'y';
    std::cout << s << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
rmx
Exemplar
Exemplary

二次

另见

at

access specified character with bounds checking (public member function)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券