Overview
In C/C++, fixed size arrays can be allocated on the stack and variable size arrays are allocated on the heap. However this is inflexible and also managing the array such as allocation and clean up, will require extra code.
The standard library provides array to address this.
Details
array is basically heap based, fixed size, contiguously arranged elements that can be treated as an array and pointer arithmetic can be performed.
Syntax
The syntax is as below. Template parameter T represents the datatype to store and N represents its capacity.
template < class T, class size_t N >class array;
Members
It defines following types
Name | Description |
---|---|
value_type | The first template parameter (T) |
allocator_type | The second template parameter (Alloc) |
reference | value_type& |
const_reference | const value_type& |
pointer | allocator_traits<allocator_type>::pointer |
const_pointer | allocator_traits<allocator_type>::const_pointer |
iterator | a random access iterator to value_type convertible to const_iterator |
const_iterator | a random access iterator to const value_type |
reverse_iterator | reverse_iterator<iterator> |
const_reverse_iterator | reverse_iterator<const_iterator> |
difference_type | a signed integral type |
size_type | an unsigned integral type |
Operation
array can be graphically represented as below. All the elements are stored in an pre allocated buffer.
Complexity
The complexity of random access operation is O(1).
Functionality
Constructors
Implicitly defined.
Examples:
array<int,5> inta; //array of integers of size 5 array<double,5> dbla; //array of doubles of size 5
Iterator
Name | Description |
---|---|
iterator begin() iterator end() | Returns iterator to beginning and end. Example: array<int,5> v{1,2,3,4,5};//prints 1 2 3 4 5for (auto itr=v.begin(); itr!=v.end(); ++itr) cout << *itr << ' '; |
reverse_iterator rbegin() reverse_iterator rend() | Returns reverse_iterator to reverse beginning and reverse end. Example: array<int,5> v{1,2,3,4,5};//prints 5 4 3 2 1for (auto itr=v.rbegin(); itr!=v.rend(); ++itr) cout << *itr << ' '; |
const_iterator cbegin() const_iterator cend() | Returns const_iterator to beginning and end. Example: array<int> v{1,2,3,4,5};//prints 1 2 3 4 5for (auto itr=v.cbegin(); itr!=v.cend(); ++itr) cout << *itr << ' '; |
const_reverse_iterator crbegin() const_reverse_iterator crend | Returns const_reverse_iterator to reverse beginning and reverse end. Example: array<int,5> v{1,2,3,4,5};//prints 5 4 3 2 1for (auto itr=v.crbegin(); itr!=v.crend(); ++itr) cout << *itr << ' '; |
Capacity
Name | Description |
---|---|
size_type size() | Returns the number of elements. Example: array<int,5> v{1,2,3,4,5};//prints 5cout << v.size(); |
size_type max_size() | Returns same as size(). |
bool empty() | Test whether array is empty. |
Element Access
Name | Description |
---|---|
reference get<size_type I>(array& a) | Returns reference or const reference to the element at position I. Compiler error upon invalid inputs. Note that get() is stand library function used with tuples. It is not a member of array class. Example: array<int,5> v{1,2,3,4,5}; |
reference operator[](size_type n) | Returns reference or const reference to the element at position n. No bounds check is done. Example: array<int,5> v{1,2,3,4,5}; |
reference at(size_type n) | Returns reference or const reference to the element at position n. Exception is thrown for invalid input. Example: array<int,5> v{1,2,3,4,5}; |
reference front() | Returns reference or const reference to the first element. Exception is thrown for invalid input.Example:array<int,5> v{1,2,3,4,5}; |
reference back() | Returns reference or const reference to the last element. Exception is thrown for invalid input.Example:array<int,5> v{1,2,3,4,5}; |
value_type* data() | Returns T* or const T* pointing to the first element in the array.Example:array<int,5> v{1,2,3,4,5}; |
Modifiers
Name | Description |
---|---|
void swap(array& v) | Swap content with v. Note T has to be same. Example: array<int,5> v{1,2,3,4,5}; array<int,5> v2{5,4,3,2,1};
|
void fill(const value_type& val) | Sets val as the value for all the elements in the array object. Example: array<int,5> v;
|
No comments:
Post a Comment