Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::unique_ptr<T,Deleter>::operator[]

      From cppreference.com
      <cpp‎ |memory‎ |unique 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)
       
       
      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

      [edit]Parameters

      i - the index of the element to be returned

      [edit]Return value

      Returns the element at indexi, i.e.get()[i].

      [edit]Example

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

      [edit]See also

      returns a pointer to the managed object
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/unique_ptr/operator_at&oldid=153940"

      [8]ページ先頭

      ©2009-2025 Movatter.jp