std::basic_string_view<CharT,Traits>:: operator[]
|
constexpr
const_reference operator
[
]
(
size_type pos
)
const
;
|
(C++17以降) | |
指定された位置 pos の文字へのconst参照を返します。
|
pos < size ( ) が false の場合、動作は未定義です。 |
(C++26まで) |
|
pos < size ( ) が false の場合:
|
(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'; // エラー: string viewを通して変更することはできません str[2] = 'y'; std::cout << v[2] << '\n'; }
出力:
e y
関連項目
|
境界チェック付きで指定された文字にアクセスする
(公開メンバ関数) |
|
|
指定された文字にアクセスする
(
std::basic_string<CharT,Traits,Allocator>
の公開メンバ関数)
|