命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | 内存‎ | shared ptr
 
 
内存管理库
(仅作说明*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
受约束的未初始化
内存算法
C 库

分配器
内存资源
垃圾回收支持
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化存储
(直到 C++20*)
(直到 C++20*)
显式生命周期管理
 
 
element_type& operator[]( std::ptrdiff_t idx ) const;
(C++17 起)

索引存储指针所指向的数组。

如果存储指针为空或 idx 为负,则行为未定义。

如果 Tshared_ptr 的模板参数)是一个数组类型 U[N],则 idx 必须小于 N,否则行为未定义。

目录

[edit] 参数

idx - 数组索引

[edit] 返回值

数组的第 idx 个元素的引用,即 get()[idx]

[edit] 异常

不抛出任何异常。

[edit] 备注

T 不是数组类型时,此函数是否声明为未指定。如果函数已声明,则其返回类型未指定,但保证函数的声明(尽管不一定是定义)是合法的。

[edit] 示例

#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

[edit] 另请参阅

返回存储的指针
(public member function) [编辑]