Ad alanları
Türevler
Eylemler

std::array

cppreference.com sitesinden
< cpp‎ | container
Bu başlık dosyasında tanımlanmıştır: <array>
template<

    class T,
    std::size_t N

> struct array;
(C++11'den beri)

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

This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member. Unlike a C-style array, it doesn't decay to T* automatically. As an aggregate type, it can be initialized with aggregate-initialization given at most N initializers that are convertible to T: 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 of Container and ReversibleContainer except that default-constructed array is not empty and that the complexity of swapping is linear, satisfies the requirements of ContiguousContainer (C++17'den beri) and partially satisfies the requirements of SequenceContainer.

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 calling front() or back() on a zero-sized array is undefined.

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

Konu başlıkları

[düzenle] Iterator invalidation

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

[düzenle] Member types

Member type Definition
value_type T [edit]
size_type std::size_t [edit]
difference_type std::ptrdiff_t [edit]
reference value_type& [edit]
const_reference const value_type& [edit]
pointer value_type*[edit]
const_pointer const value_type*[edit]
iterator RandomAccessIterator and ConstexprIterator (C++20'den beri)that is a LiteralType (C++17'den beri) [edit]
const_iterator Constant RandomAccessIterator and ConstexprIterator (C++20'den beri)that is a LiteralType (C++17'den beri) [edit]
reverse_iterator std::reverse_iterator<iterator> [edit]
const_reverse_iterator std::reverse_iterator<const_iterator> [edit]

[düzenle] Member functions

Implicitly-defined member functions
(yapıcı fonksiyon)
(dolaylı olarak tanımlandı)
initializes the array following the rules of aggregate initialization (note that default initialization may result in indeterminate values for non-class T)
(herkese açık üye fonksiyon)
(yıkıcı fonksiyon)
(dolaylı olarak tanımlandı)
destroys every element of the array
(herkese açık üye fonksiyon)
operator=
(dolaylı olarak tanımlandı)
overwrites every element of the array with the corresponding element of another array
(herkese açık üye fonksiyon)
Element access
belirtilen öğeye aralık kontrolü yaparak erişir
(herkese açık üye fonksiyon) [edit]
belirtilen öğeye erişir
(herkese açık üye fonksiyon) [edit]
ilk öğeye erişir
(herkese açık üye fonksiyon) [edit]
sondaki öğeye erişir
(herkese açık üye fonksiyon) [edit]
içindeki C-tarzı diziye doğrudan erişim sağlar
(herkese açık üye fonksiyon) [edit]
Iterators
ilk öğeye işaret eden bir yineleyici döndürür
(herkese açık üye fonksiyon) [edit]
sondaki öğeye işaret eden bir işaretçi döndürür
(herkese açık üye fonksiyon) [edit]
returns a reverse iterator to the beginning
(herkese açık üye fonksiyon) [edit]
returns a reverse iterator to the end
(herkese açık üye fonksiyon) [edit]
Capacity
checks whether the container is empty
(herkese açık üye fonksiyon) [edit]
returns the number of elements
(herkese açık üye fonksiyon) [edit]
returns the maximum possible number of elements
(herkese açık üye fonksiyon) [edit]
Operations
fill the container with specified value
(herkese açık üye fonksiyon) [edit]
swaps the contents
(herkese açık üye fonksiyon) [edit]

[düzenle] Non-member functions

lexicographically compares the values in the array
(fonksiyon şablonu) [edit]
accesses an element of an array
(fonksiyon şablonu) [edit]
specializes the std::swap algorithm
(fonksiyon şablonu) [edit]

[düzenle] Helper classes

obtains the size of an array
(sınıf şablonu özelleştirmesi) [edit]
obtains the type of the elements of array
(sınıf şablonu özelleştirmesi) [edit]

[düzenle] Deduction guides(C++17'den beri)

[düzenle] Example

#include <string>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <array>
 
int main()
{
    // construction uses aggregate initialization
    std::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 (not in C++14)
    std::array<int, 3> a2 = {1, 2, 3};  // never required after =
    std::array<std::string, 2> a3 = { std::string("a"), "b" };
 
    // container operations are supported
    std::sort(a1.begin(), a1.end());
    std::reverse_copy(a2.begin(), a2.end(), 
                      std::ostream_iterator<int>(std::cout, " "));
 
    std::cout << '\n';
 
    // ranged for loop is supported
    for(const auto& s: a3)
        std::cout << s << ' ';
}

Output:

3 2 1 
a b

[düzenle] See also

Creates a std::array object whose size and optionally element type are deduced from the arguments
(fonksiyon şablonu) [edit]
Creates a std::array object from a built-in array
(fonksiyon şablonu) [edit]