std::unique_ptr::operator[]

T& operator[](size_t i) const;
(C++11 起)

operator[] 提供由 unique_ptr 管理的数组元素的访问。

i 是必需的参数,是一个有效的数组索引。

目录

参数

i - 要返回的元素的索引

返回值

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

示例

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

输出:

0: 1
1: 1
2: 2
3: 6
4: 24
5: 120
6: 720
7: 5040
8: 40320
9: 362880

另请参阅

返回指向被管理对象的指针
(公开成员函数)

版本历史

  • (当前 | 先前 2017年6月14日 (三) 21:532402:f000:1:3c00:d90:d100:a474:e81a讨论. . (1,315字节) (-276). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:58P12bot讨论 | 贡献 . . (1,591字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:08P12bot讨论 | 贡献 . . (1,591字节) (-89). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 14:13P12bot讨论 | 贡献 . . (1,680字节) (+329). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 03:41P12讨论 | 贡献 . . (1,351字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,351字节) (-42). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:56P12讨论 | 贡献 . . (1,393字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,393字节) (+1,393). . (Translated from the English version using Google Translate)