|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Modifiers | ||||
| Observers | ||||
unique_ptr::operator[] | ||||
| Non-member functions | ||||
(C++14)(C++20) | ||||
(until C++20)(C++20) | ||||
(C++20) | ||||
| Helper classes | ||||
T& operator[](std::size_t i)const; | (since C++11) (constexpr since C++23) | |
operator[] provides access to elements of an array managed by aunique_ptr.
The parameteri 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 |
| i | - | the index of the element to be returned |
Returns the element at indexi, i.e.get()[i].
#include <iostream>#include <memory> int main(){constint size=10;std::unique_ptr<int[]> fact(newint[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! = 11! = 12! = 23! = 64! = 245! = 1206! = 7207! = 50408! = 403209! = 362880
| returns a pointer to the managed object (public member function)[edit] |