|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Modifiers | ||||
| Observers | ||||
shared_ptr::operator[] (C++17) | ||||
(until C++20*) | ||||
(C++26) | ||||
(C++26) | ||||
| Non-member functions | ||||
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20) | ||||
functions(until C++26*) | ||||
| Helper classes | ||||
(C++20) | ||||
| Deduction guides(C++17) |
element_type& operator[](std::ptrdiff_t idx)const; | (since C++17) | |
Index into the array pointed to by the stored pointer.
The behavior is undefined if the stored pointer is null or ifidx is negative.
IfT (the template parameter ofshared_ptr) is an array typeU[N],idx must be less thanN, otherwise the behavior is undefined.
Contents |
| idx | - | the array index |
A reference to theidx-th element of the array, i.e.,get()[idx].
Throws nothing.
WhenT is not an array type, it is unspecified whether this function is declared. If the function is declared, it is unspecified what its return type is, except that the declaration (although not necessarily the definition) of the function is guaranteed to be legal.
#include <cstddef>#include <iostream>#include <memory> int main(){conststd::size_t arr_size=10;std::shared_ptr<int[]> pis(newint[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';}
Output:
0 1 2 3 4 5 6 7 8 9
| returns the stored pointer (public member function)[edit] |