首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::unique_ptr::operator[]

T& operator const;

(since C++11)

operator[]提供对由unique_ptr...

参数i应小于数组中元素的数量;否则,行为未定义。

参数

i

-

the index of the element to be returned

返回值

返回索引处的元素i,即.get()[i]...

二次

代码语言:javascript
复制
#include <iostream>
#include <memory>
 
int main() 
{
    const int size = 10; 
    std::unique_ptr<int[]> fact(new int[size]);
 
    for (int i = 0; i < size; ++i) {
        fact[i] = (i == 0) ? 1 : i * fact[i-1];
    }
 
    for (int i = 0; i < size; ++i) {
        std::cout << i << ": " << fact[i] << '\n';
    }
}

二次

产出:

二次

代码语言:javascript
复制
0: 1
1: 1
2: 2
3: 6
4: 24
5: 120
6: 720
7: 5040
8: 40320
9: 362880

二次

另见

get

returns a pointer to the managed object (public member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券