Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::array

      From cppreference.com
      <cpp‎ |container
       
       
       
       
      Defined in header<array>
      template<

         class T,
         std::size_t N

      >struct array;
      (since C++11)

      std::array is a container that encapsulates fixed size arrays.

      This container is an aggregate type with the same semantics as a struct holding aC-style arrayT[N] as its only non-static data member. Unlike a C-style array, it doesn't decay toT* automatically. As an aggregate type, it can be initialized withaggregate-initialization given at mostN initializers that are convertible toT:std::array<int,3> a={1,2,3};.

      The struct combines the performance and accessibility of a C-style array with the benefits of a standard container, such as knowing its own size, supporting assignment, random access iterators, etc.

      std::array satisfies the requirements ofContainer andReversibleContainer except that default-constructed array is not empty and that the complexity of swapping is linear,satisfies the requirements ofContiguousContainer,(since C++17) and partially satisfies the requirements ofSequenceContainer.

      There is a special case for a zero-length array (N == 0). In that case,array.begin()== array.end(), which is some unique value. The effect of callingfront() orback() on a zero-sized array is undefined.

      An array can also be used as a tuple ofN elements of the same type.

      Contents

      [edit]Iterator invalidation

      As a rule, iterators to an array are never invalidated throughout the lifetime of the array. One should take note, however, that duringswap, the iterator will continue to point to the same array element, and will thus change its value.

      [edit]Template parameters

      T - element type Must beMoveConstructible andMoveAssignable.
      N - the number of elements in the array or0.
      This section is incomplete
      Reason: Complete the descriptions of template parameters.

      [edit]Member types

      Member type Definition
      value_typeT[edit]
      size_typestd::size_t[edit]
      difference_typestd::ptrdiff_t[edit]
      referencevalue_type&[edit]
      const_referenceconst value_type&[edit]
      pointervalue_type*[edit]
      const_pointerconst value_type*[edit]
      iterator

      LegacyRandomAccessIterator andLegacyContiguousIterator tovalue_type

      (until C++17)

      LegacyRandomAccessIterator andLegacyContiguousIterator that is aLiteralType tovalue_type

      (since C++17)
      (until C++20)

      LegacyRandomAccessIterator,contiguous_iterator, andConstexprIterator tovalue_type

      (since C++20)
      [edit]
      const_iterator

      LegacyRandomAccessIterator andLegacyContiguousIterator toconst value_type

      (until C++17)

      LegacyRandomAccessIterator andLegacyContiguousIterator that is aLiteralType toconst value_type

      (since C++17)
      (until C++20)

      LegacyRandomAccessIterator,contiguous_iterator, andConstexprIterator toconst value_type

      (since C++20)
      [edit]
      reverse_iteratorstd::reverse_iterator<iterator>[edit]
      const_reverse_iteratorstd::reverse_iterator<const_iterator>[edit]

      [edit]Member functions

      Implicitly-defined member functions
      (constructor)
      (implicitly declared)
      initializes the array following the rules ofaggregate initialization (note that default initialization may result in indeterminate values for non-classT)
      (public member function)
      (destructor)
      (implicitly declared)
      destroys every element of the array
      (public member function)
      operator=
      (implicitly declared)
      overwrites every element of the array with the corresponding element of another array
      (public member function)
      Element access
      access specified element with bounds checking
      (public member function)[edit]
      access specified element
      (public member function)[edit]
      access the first element
      (public member function)[edit]
      access the last element
      (public member function)[edit]
      direct access to the underlying contiguous storage
      (public member function)[edit]
      Iterators
      returns an iterator to the beginning
      (public member function)[edit]
      returns an iterator to the end
      (public member function)[edit]
      returns a reverse iterator to the beginning
      (public member function)[edit]
      returns a reverse iterator to the end
      (public member function)[edit]
      Capacity
      checks whether the container is empty
      (public member function)[edit]
      returns the number of elements
      (public member function)[edit]
      returns the maximum possible number of elements
      (public member function)[edit]
      Operations
      fill the container with specified value
      (public member function)[edit]
      swaps the contents
      (public member function)[edit]

      [edit]Non-member functions

      (C++11)(C++11)(removed in C++20)(C++11)(removed in C++20)(C++11)(removed in C++20)(C++11)(removed in C++20)(C++11)(removed in C++20)(C++20)
      lexicographically compares the values of twoarrays
      (function template)[edit]
      accesses an element of anarray
      (function template)[edit]
      specializes thestd::swap algorithm
      (function template)[edit]
      (C++20)
      creates astd::array object from a built-in array
      (function template)[edit]

      [edit]Helper classes

      obtains the size of anarray
      (class template specialization)[edit]
      obtains the type of the elements ofarray
      (class template specialization)[edit]

      Deduction guides

      (since C++17)

      [edit]Example

      Run this code
      #include <algorithm>#include <array>#include <iostream>#include <iterator>#include <string> int main(){// Construction uses aggregate initialization    std::array<int,3> a1{{1,2,3}};// Double-braces required in C++11 prior to// the CWG 1270 revision (not needed in C++11// after the revision and in C++14 and beyond)     std::array<int,3> a2={1,2,3};// Double braces never required after = // Container operations are supportedstd::sort(a1.begin(), a1.end());    std::ranges::reverse_copy(a2,std::ostream_iterator<int>(std::cout," "));std::cout<<'\n'; // Ranged for loop is supported    std::array<std::string,2> a3{"E","\u018E"};for(constauto& s: a3)std::cout<< s<<' ';std::cout<<'\n'; // Deduction guide for array creation (since C++17)[[maybe_unused]] std::array a4{3.0,1.0,4.0};// std::array<double, 3> // Behavior of unspecified elements is the same as with built-in arrays[[maybe_unused]] std::array<int,2> a5;// No list init, a5[0] and a5[1]// are default initialized[[maybe_unused]] std::array<int,2> a6{};// List init, both elements are value// initialized, a6[0] = a6[1] = 0[[maybe_unused]] std::array<int,2> a7{1};// List init, unspecified element is value// initialized, a7[0] = 1, a7[1] = 0}

      Output:

      3 2 1E Ǝ

      [edit]See also

      resizable, fixed capacity, inplace contiguous array
      (class template)[edit]
      resizable contiguous array
      (class template)[edit]
      double-ended queue
      (class template)[edit]
      (library fundamentals TS v2)
      creates astd::array object whose size and optionally element type are deduced from the arguments
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/array&oldid=174050"

      [8]ページ先頭

      ©2009-2025 Movatter.jp