C + + _STL – array (c + + 11)

Reference: array

Class template

template < class T, size_t N > class array;

1.1 Container Properties

Container attribute
The sequence The elements in a sequence container are sorted in a strict linear order. Individual elements are accessed by their position in the sequence.
Continuous storage These elements are stored in contiguous memory locations, allowing constant time random access to the elements. Pointers to elements can be offset to access other elements.
Fixed size The container uses implicit constructors and destructors to statically allocate the required space. Its size is a compile-time constant. No memory or time overhead.

1.2 Template Parameters

Template parameter
T The type of the contained element.

The alias is a member typeArray: : value_type.
N The size of an array, expressed as a number of elements.

1.3 example

std::array<int,10> myarray;
Copy the code

2, STD: : array: : the at

reference at ( size_type n );
const_reference at ( size_type n ) const;
Copy the code

Access element

2.1 features

Returns a reference to the element at position N in the array. This function automatically checks if n is within the range of valid elements in the container, and if it is not (that is, n is greater than or equal to its size), an out_of_range exception is thrown. This is in sharp contrast to the member operator [], who does not check boundaries.

2.2 parameter

parameter
n The position of an element in an array. If this is greater than or equal to the array size, an exception of type out_of_range is thrown. Notice that the first element is in position 0 (not 1). The member type size_type is an alias of the unsigned integral type size_t.

2.3 the return value

The element in the array at the specified position. If the array object is const qualified, the function returns a const_reference. Otherwise, it returns a reference.

// array::at
#include <iostream>
#include <array>

int main (a)
{
  std::array<int,10> myarray;

  // assign some values:
  for (int i=0; i<10; i++) myarray.at(i) = i+1;

  // print content:
  std::cout << "myarray contains:";
  for (int i=0; i<10; i++)
    std::cout << ' ' << myarray.at(i);
  std::cout << '\n';

  return 0;
}
Copy the code

3, STD: : array: : back

reference back(a);
const_reference back(a) const;
Copy the code

3.1 features

Returns a reference to the last element in the array container

3.2 the return value

A reference to the last element in an array. If the array object is const qualified, the function returns a const_reference. Otherwise, it returns a reference.

// array::back
#include <iostream>
#include <array>

int main (a)
{
  std::array<int,3> myarray = {5.19.77};

  std::cout << "front is: " << myarray.front() << std::endl;   / / 5
  std::cout << "back is: " << myarray.back() << std::endl;     / / 77

  myarray.back() = 50;

  std::cout << "myarray now contains:";
  for ( int& x : myarray ) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}
Copy the code

4, STD: : array: : begin

iterator begin(a) noexcept;
const_iterator begin(a) const noexcept;
Copy the code

4.1 features

Returns an iterator that refers to the first element in an array container. You can modify what the iterator refers to

4.2 the return value

An iterator that refers to the first element in an array. If the array object is const qualified, the function returns a const_iterator. Otherwise, it returns an iterator.

// array::begin example
#include <iostream>
#include <array>

int main (a)
{
  std::array<int,5> myarray = { 2.16.77.34.50 };

  std::cout << "myarray contains:";
  for ( auto it = myarray.begin(a); it ! = myarray.end(a); ++it ){ *it+=1;
      std::cout << ' ' << *it;
  std::cout << '\n';
      } 
  return 0;
}
Copy the code

5, STD: : array: : cbegin

const_iterator cbegin(a) const noexcept;
Copy the code

5.1 features

Returns a const_iterator that refers to the first element in the array container

5.2 the return value

Array refers to the first element, const_iterator.

// array::cbegin example
#include <iostream>
#include <array>

int main (a)
{
  std::array<int,5> myarray = { 2.16.77.34.50 };

  std::cout << "myarray contains:";

  for ( auto it = myarray.cbegin(a); it ! = myarray.cend(a); ++it ) std::cout <<' ' << *it;   // cannot modify *it

  std::cout << '\n';

  return 0;
}
Copy the code

6, STD: : array: : cend

const_iterator cend(a) const noexcept;
Copy the code

6.1 features

Returns a past-the-end const_iteratorr from an array container.

7, STD: : array: : crbegin

const_reverse_iterator crbegin(a) const noexcept;
Copy the code

7.1 features

Return a const_reverse_iterator that refers to the last element in an array. It cannot be changed. A CONST_reverse_iterator is an iterator that refers to const contents and iterates in reverse order.

auto rit=myarray.crbegin() ; rit < myarray.crend(); + + rit not rit –

7.2 the return value

A const_reverse_iterator that refers to the last element in an array.

// array::crbegin/crend
#include <iostream>
#include <array>

int main (a)
{
  std::array<int,6> myarray = {10.20.30.40.50.60}; std::cout <<"myarray backwards:";
  for ( auto rit=myarray.crbegin() ; rit < myarray.crend(a); ++rit )//rit ! = myarray on. Crend () can also be
    std::cout << ' ' << *rit;   // cannot modify *rit

  std::cout << '\n';

  return 0;
}
Copy the code

8, STD: : array: : crend

const_reverse_iterator crend() const noexcept;
Copy the code

8.1 features

Return a const_reverse_iterator that points to the theoretical element before the first element in the vector, which is treated as its reverse endpoint.

9, STD: : array: : data

value_type* data(a) noexcept;
const value_type* data(a) const noexcept;
Copy the code

9.1 features

Returns a pointer to the header element.

Because the elements in the array are stored in adjacent storage locations, the retrieved pointer can be offset to access any element in the array.

// array::data
#include <iostream>
#include <cstring>
#include <array>

int main (a)
{
  const char* cstr = "Test string";
  std::array<char,12> charray;

  std::memcpy (charray.data(),cstr,12);

  std::cout << charray.data() < <'\n';

  return 0;
}
Copy the code

10, STD: : array: : empty

constexpr bool empty(a) noexcept;
Copy the code

10.1 features

Check whether the array is empty

10.2 the return value

True if the array size is 0, false otherwise.

// array::empty
#include <iostream>
#include <array>

int main (a)
{
  std::array<int,0> first;
  std::array<int,5> second;
  std::cout << "first " << (first.empty()?"is empty" : "is not empty") < <'\n';
  std::cout << "second " << (second.empty()?"is empty" : "is not empty") < <'\n';
  return 0;
}
Copy the code

11, STD: : array: : end

iterator end(a) noexcept;
const_iterator end(a) const noexcept;
Copy the code

11.1 features

Returns a past-the-end iteratorr in an array container. , you can modify what the iterator points to

12, STD: : array: : the fill

void fill (const value_type& val);
Copy the code

12.1 features

Sets val to the value of all elements in the array object.

12.2 parameter

parameter
val Value to populate the array.
// array::fill example
#include <iostream>
#include <array>

int main (a) {
  std::array<int,6> myarray;

  myarray.fill(5);

  std::cout << "myarray contains:";
  for ( int& x : myarray) { std::cout << ' ' << x; }

  std::cout << '\n';

  return 0;
}
Copy the code

13, STD: : array: : front

reference front(a);
const_reference front(a) const;
Copy the code

13, 1 function

Returns the first value reference.

13.2 the return value

The reference to the first element in an array. If the array object is const qualified, the function returns a const_reference. Otherwise, it returns a reference.

// array::front
#include <iostream>
#include <array>

int main ()
{
  std::array<int,3> myarray = {2, 16, 77};

  std::cout << "front is: " << myarray.front() << std::endl;   // 2
  std::cout << "back is: " << myarray.back() << std::endl;     // 77

  myarray.front() = 100;

  std::cout << "myarray now contains:";
  for ( int& x : myarray ) std::cout << ' ' << x;

  std::cout << '\n';

  return 0;
}
Copy the code

14, STD: : array: : max_size

constexpr size_type max_size(a) noexcept;
Copy the code

14.1 features

Returns the maximum number of elements that an array container can hold.

The maximum size of an array object is always equal to the second template parameter used to instantiate the array template class, as is its size.

// array::max_size
#include <iostream>
#include <array>

int main (a)
{
  std::array<int,10> myints;
  std::cout << "size of myints: " << myints.size() < <'\n';
  std::cout << "max_size of myints: " << myints.max_size() < <'\n';

  return 0;
}
Copy the code

15, STD: : array: : operator []

reference operator[] (size_type n);
const_reference operator[] (size_type n) const;
Copy the code

15.1 features

Returns a reference to the element at position N in the array container. A similar member function array:: AT behaves the same as this operator function, except array:: AT checks array boundaries and indicates whether n is out of range by throwing an exception. An error occurs when this member function goes beyond the bounds. Returns const_reference if the array has a const modifier

15.2 the return value

parameter
n The position of an element in an array.
// array::operator[]
#include <iostream>
#include <array>

int main (a)
{
  std::array<int,10> myarray;
  unsigned int i;

  // assign some values:
  for (i=0; i<10; i++) myarray[i]=i;

  // print content
  std::cout << "myarray contains:";
  for (i=0; i<10; i++)
    std::cout << ' ' << myarray[i];
  std::cout << '\n';

  return 0;
}
Copy the code

16, STD: : array: : rbegin

reverse_iterator rbegin(a) noexcept;
const_reverse_iterator rbegin(a) const noexcept;
Copy the code

16.1 features

Returns an iterator that refers to the last element in the array container. We can modify what the iterator refers to, but the const qualifier cannot.

16.2 the return value

A reverse iterator that reverses the beginning of a sequence. If the array object is const qualified, the function returns a const_iterator. Otherwise, it returns an iterator.

The member types reverse_iterator and CONST_reverse_iterator are the reverse random-access iterator types (referring to elements and constant elements, respectively).

// array::rbegin/rend
#include <iostream>
#include <array>

int main (a)
{
  std::array<int,4> myarray = {4.26.80.14}; std::cout <<"myarray contains:";
  for ( auto rit=myarray.rbegin() ; rit < myarray.rend(a); ++rit ) std::cout <<' ' << *rit;

  std::cout << '\n';

  return 0;
}
Copy the code

17, STD: : array: : rend

reverse_iterator rend(a) noexcept;
const_reverse_iterator rend(a) const noexcept;
Copy the code

17.1 features

Returns a reverse iterator that refers to the theoretical element before the first element in the array (which is treated as its reverse end).

17.2 the return value

The reverse iterator at the reverse end of the sequence. If the array object is const qualified, the function returns a const_iterator. Otherwise, it returns an iterator.

The member types reverse_iterator and CONST_reverse_iterator are the reverse random-access iterator types (referring to elements and constant elements, respectively).

18, STD: : array: : size

constexpr size_type size(a) noexcept;
Copy the code

18.1 features

Returns the number of elements in an array container. The size of the array object is always equal to the second template parameter used to instantiate the array template class (N). Unlike the language operator sizeof, which returns the size in bytes, this member function returns the sizeof the array in elements.

// array::size
#include <iostream>
#include <array>

int main (a)
{
  std::array<int,5> myints;
  std::cout << "size of myints: " << myints.size() << std::endl;
  std::cout << "sizeof(myints): " << sizeof(myints) << std::endl;

  return 0;
}
Copy the code

19, STD: : array: : swap

void swap (array& x) noexcept(noexcept(swap(declval<value_type&>(),declval<value_type&>())));
Copy the code

19.1 features

Swap the contents of the array for the contents of x, which is another array object of the same type (size). After the member function is called, the elements in the container are the elements in x before the call, and the elements of X are the elements in the container. Unlike swap member functions in other containers, this member function runs in linear time by performing a single swap operation of the same size between individual elements (see swap).

19.2 complexity

Linear in size of the container.


O ( n ) O(n)

N indicates the size of the container

// swap arrays
#include <iostream>
#include <array>

int main (a)
{
  std::array<int,5> first = {10.20.30.40.50};
  std::array<int,5> second = {11.22.33.44.55};

  first.swap (second);

  std::cout << "first:";
  for (int& x : first) std::cout << ' ' << x;
  std::cout << '\n';

  std::cout << "second:";
  for (int& x : second) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}
Copy the code

non-member overloads:

std::get (array)

template <size_t I, class T.size_t N> T& get (array<T,N>& arr) noexcept;
template <size_t I, class T.size_t N> T&& get (array<T,N>&& arr) noexcept;
template <size_t I, class T.size_t N> const T& get (const array<T,N>& arr) noexcept;
Copy the code

Function of 1.

Returns a reference to the ith element of the array ARr.

Parameters of 2.

Pass I and arr–An array container

3. The return value

A reference to an element at a specified position in an array.

// arrays as tuples
#include <iostream>
#include <array>
#include <tuple>

int main (a)
{
  std::array<int,3> myarray = {10.20.30};
  std::tuple<int.int.int> mytuple (10.20.30);

  std::tuple_element<0.decltype(myarray)>::type myelement;  // int myelement

  myelement = std::get<2>(myarray);
  std::get<2>(myarray) = std::get<0>(myarray);
  std::get<0>(myarray) = myelement;

  std::cout << "first element in myarray: " << std::get<0>(myarray) << "\n";
  std::cout << "first element in mytuple: " << std::get<0>(mytuple) << "\n";

  return 0;
}
Copy the code

std::relational operators (array)

(1) template <class T, size_T N> bool operator== ( const array<T,N>& lhs, const array<T,N>& rhs );
(2) template <class T, size_T N> bool operator! = ( const array<T,N>& lhs, const array<T,N>& rhs );
(3) template <class T, size_T N> bool operator< ( const array<T,N>& lhs, const array<T,N>& rhs );
(4) template <class T, size_T N> bool operator<= ( const array<T,N>& lhs, const array<T,N>& rhs );
(5) template <class T, size_T N> bool operator> ( const array<T,N>& lhs, const array<T,N>& rhs );
(6) template <class T, size_T N> bool operator>= ( const array<T,N>& lhs, const array<T,N>& rhs );

Function of 1.

Compare sizes, same ==, different! Is the first difference between T and N greater than N, is the first difference between T and N less than N

Parameters of 2.

two array container.

3. The return value

True or false.

// array comparisons
#include <iostream>
#include <array>

int main (a)
{
  std::array<int,5> a = {10.20.30.40.50};
  std::array<int,5> b = {10.20.30.40.50};
  std::array<int,5> c = {50.40.30.20.10};

  if (a==b) std::cout << "a and b are equal\n";
  if(b! =c) std::cout <<"b and c are not equal\n";
  if (b<c) std::cout << "b is less than c\n";
  if (c>b) std::cout << "c is greater than b\n";
  if (a<=b) std::cout << "a is less than or equal to b\n";
  if (a>=b) std::cout << "a is greater than or equal to b\n";

  return 0;
}
Copy the code

std::swap(std::array)

Function of 1.

Same complexity as STD :: Array ::swap

Parameters of 2.

two array container.