Namespaces
Variants

std::shared_ptr<T>:: operator[]

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
element_type & operator [ ] ( std:: ptrdiff_t idx ) const ;
(C++17以降)

格納されたポインタが指す配列へのインデックス指定。

格納されているポインタがnullの場合、または idx が負の値である場合、動作は未定義です。

T shared_ptr のテンプレートパラメータ)が配列型 U[N] の場合、 idx N 未満でなければならず、そうでない場合の動作は未定義です。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - HTMLタグ、属性、クラス名はすべて保持されています - ` `内のテキストはC++関連の用語(Parameters、Return value、Exceptions、Remarks、Example、See also)であるため、翻訳せずに保持しています - 番号部分と書式は完全に保持されています - プロフェッショナルな技術文書としての正確性を維持しています

パラメータ

idx - 配列インデックス

戻り値

配列の idx 番目の要素への参照、すなわち get ( ) [ idx ] です。

例外

例外を送出しません。

備考

T が配列型でない場合、この関数が宣言されるかどうかは未規定です。関数が宣言される場合、その戻り値の型は未規定ですが、関数の宣言(定義は必ずしもそうではない)が合法であることが保証されます。

#include <cstddef>
#include <iostream>
#include <memory>
int main()
{
    const std::size_t arr_size = 10;
    std::shared_ptr<int[]> pis(new int[10]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
    for (std::size_t i = 0; i < arr_size; ++i)
        std::cout << pis[i] << ' ';
    std::cout << '\n';
}

出力:

0 1 2 3 4 5 6 7 8 9

関連項目

格納されたポインタを返す
(公開メンバ関数)