名稱空間
變體
操作

std::basic_string_view<CharT,Traits>::operator[]

來自 cppreference.com
 
 
 
 
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> 的公共成員函式) [編輯]