std::unique_ptr<T,Deleter>::operator[]
From cppreference.com
< cpp | memory | unique ptr
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Memory management library
(exposition only*)
(C++11)
(C++23)
(C++11)
(C++17)
(C++11)
(C++11)
(C++20)
(C++20)
(C++17)
(C++11)
(C++17)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
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)
(C++11)
(C++11)
(C++17)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
(until C++17*)
(C++11)
(C++17)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
std::unique_ptr
unique_ptr::operator[]
(C++14)(C++20)
(until C++20)(C++20)
(C++20)
operator[]
provides access to elements of an array managed by a unique_ptr
.
The parameter i shall be less than the number of elements in the array; otherwise, the behavior is undefined.
This member function is only provided for specializations for array types.
Contents
[edit] Parameters
i
-
the index of the element to be returned
[edit] Return value
Returns the element at index i, i.e. get()[i]
.
[edit] Example
Run this code
#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'; }
Output:
0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880