◐ Shell
clean mode source ↗

std::basic_string<CharT,Traits,Allocator>::operator[] - cppreference.com

提供: cppreference.com

<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>

(1)

reference operator[]( size_type pos );

(C++20未満)

constexpr reference operator[]( size_type pos );

(C++20以上)
(2)

const_reference operator[]( size_type pos ) const;

(C++20未満)

constexpr const_reference operator[]( size_type pos ) const;

(C++20以上)

指定された位置 pos の文字を指す参照を返します。 範囲チェックは行われません。 pos > size() の場合、動作は未定義です。

1) pos == size() の場合、動作は未定義です。

2) pos == size() の場合、値 CharT() (ヌル文字) を指す参照が返されます。

(C++11未満)

pos == size() の場合、値 CharT() (ヌル文字) を指す参照が返されます。

最初の (非 const の) バージョンでは、この文字が CharT() 以外の何らかの値に変更された場合、動作は未定義です 。

(C++11以上)

引数

戻り値

要求された文字を指す参照。

計算量

一定。

#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';
}

出力:

関連項目

境界チェック付きで指定された文字にアクセスします
(パブリックメンバ関数) [edit]