Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::shared_ptr<T>::operator[]

      From cppreference.com
      <cpp‎ |memory‎ |shared ptr
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(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)
       
      std::shared_ptr
      Member functions
      Modifiers
      Observers
      shared_ptr::operator[]
      (C++17)
      (until C++20*)
      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
      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

      [edit]Parameters

      idx - the array index

      [edit]Return value

      A reference to theidx-th element of the array, i.e.,get()[idx].

      [edit]Exceptions

      Throws nothing.

      [edit]Remarks

      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.

      [edit]Example

      Run this code
      #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

      [edit]See also

      returns the stored pointer
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/shared_ptr/operator_at&oldid=153790"

      [8]ページ先頭

      ©2009-2025 Movatter.jp