- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library - <array>
Introduction
Arrays are sequence container of fixed size. Container is a objects that holds data of same type. Sequence containers store elements strictly in linear sequence.
The container class uses implicit constructor to allocate required memory statically. Memory is allocated at the compile time, hence array size cannot shrink or expand at runtime. All elements inside array are located at contiguous memory locations.
Definition
Below is definition of std::array from <array> header file.
template < class T, size_t N > class array;
Parameters
-
T − Type of the element contained.
T may be substituted by any other data type including user-defined type.
-
N − Size of the array.
Zero sized arrays are also valid. In that case array.begin() and array.end() points to same location. But behavior of calling front() or back() is undefined.
Member types
Following member types can be used as parameters or return type by member functions.
| Sr.No. | Member types | Definition |
|---|---|---|
| 1 | value_type | T (First parameter of the template) |
| 2 | reference | value_type& |
| 3 | const_reference | const value_type& |
| 4 | pointer | value_type* |
| 5 | const_pointer | const value_type* |
| 6 | iterator | a random access iterator to value_type |
| 7 | const_iterator | a random access iterator to const value_type |
| 8 | reverse_iterator | std::reverse_iterator <iterator> |
| 9 | const_reverse_iterator | std::reverse_iterator <const_iterator> |
| 10 | size_type | size_t |
| 11 | difference_type | ptrdiff_t |
Functions from <array>
Below is list of all methods from <array> header.
Member functions
| Sr.No. | Method & Description |
|---|---|
| 1 | array::at Returns a reference to the element present at location N in given array container. |
| 2 | array::back Returns a reference to the last element of the array container. |
| 3 | array::begin Returns an iterator which points to the start of the array. |
| 4 | array::cbegin Returns a constant iterator which points to the start of the array. |
| 5 | array::cend Returns a constant iterator which points to the past-end element of array. |
| 6 | array::crbegin Returns a constant reverse iterator pointing to the last element of the array. |
| 7 | array::crend Returns a constant reverse iterator which points to the past-end. |
| 8 | array::data Return a pointer pointing to the first element of the array container. |
| 9 | array::empty Tests whether size of array is zero or not. |
| 10 | array::end Returns an iterator which points to the past-end element of array. |
| 11 | array::fill Sets given value to all elements of array. |
| 12 | array::front Returns a reference to the first element of the array container. |
| 13 | array::max_size Returns the maximum number of elements that can be held by array container. |
| 14 | array::operator[] Returns a reference to the element present at location N in a given array container. |
| 15 | array::rbegin Returns a reverse iterator pointing to the last element of the array. |
| 16 | array::rend Returns a reverse iterator which points to the theoretical element preceding to first element of the array. |
| 17 | array::size Returns the number of elements present in the array. |
| 18 | array::swap Swap the contents of the two array. |
Non-member overloaded functions
| Sr.No. | Method & Description |
|---|---|
| 1 | get(array) Returns reference to the Ith element of the array container. |
| 2 | bool operator== Tests whether two containers are identical or not |
| 3 | bool operator!= Tests whether two containers are identical or not |
| 4 | bool operator< Tests whether first array container is less than second or not. |
| 5 | bool operator<= Tests whether first array container is less than or equal to second or not. |
| 6 | bool operator> Tests whether first array container is greater than second or not. |
| 7 | bool operator>= Tests whether first array container is greater than or equal to second or not. |
Non-member specilization functions
| Sr.No. | Method & Description |
|---|---|
| 1 | tuple_element(array) Provides compile-type indexed access to the type of the elements of the array using tuple-like interface. |
| 2 | tuple_size(array) Returns the total number of elements present in the container. |