std::experimental::basic_string_view<CharT,Traits>::operator[]
來自 cppreference.com
< cpp | experimental | basic string view
constexpr const_reference operator[](size_type pos) const; |
(庫基礎 TS) | |
返回指定位置 `pos` 處字元的 const 引用。
不執行邊界檢查:如果 pos >= size(),則行為未定義。
目錄 |
[編輯] 引數
pos | - | 要返回的字元的位置 |
[編輯] 返回值
對請求字元的 const 引用
[編輯] 異常
不丟擲
[編輯] 複雜度
常數時間。
[編輯] 注意
與 std::basic_string::operator[] 不同,`basic_string_view::operator[](size())` 具有未定義行為,而不是返回 `CharT()`。
[編輯] 示例
執行此程式碼
#include <iostream> #include <experimental/string_view> int main() { std::string str = "Exemplar"; std::experimental::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 成員函式) |