Tuesday, August 27, 2024

array

 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 represents the datatype to store and represents its capacity.
template < class T, class size_t N > 
class array;

Members
 It defines following types
NameDescription
value_typeThe first template parameter (T)
allocator_typeThe second template parameter (Alloc)
referencevalue_type&
const_referenceconst value_type&
pointerallocator_traits<allocator_type>::pointer
const_pointerallocator_traits<allocator_type>::const_pointer
iteratora random access iterator to value_type convertible to const_iterator
const_iteratora random access iterator to const value_type
reverse_iteratorreverse_iterator<iterator>
const_reverse_iteratorreverse_iterator<const_iterator>
difference_typea signed integral type
size_typean 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
NameDescription
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 5
for (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 1
for (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 5
for (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 1
for (auto itr=v.crbegin(); itr!=v.crend(); ++itr)
    cout << *itr << ' ';

Capacity
NameDescription
size_type size()Returns the number of elements.

Example:
array<int,5> v{1,2,3,4,5};
//prints 5
cout << v.size();
size_type max_size()Returns same as size().
bool empty()Test whether array is empty.

Element Access
NameDescription
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};
//prints 2 
cout << get<1>(v); 
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};
//prints 2 nnn. no exception checks
cout << v[1] << " " << v[100]; 
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};
//prints 2. exception is thrown.
cout << v.at(1) << " " << v(100);
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};
//prints 2. 
cout << v.front(); 
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};
//prints 5. 
cout << v.back(); 
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};
//prints 2. 
cout << *(v.data()+1); 

Modifiers
NameDescription
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};
//v2:{1,2,3,4,5}
//v:{1,2,3,4,5}
v2.swap(v);
void fill(const value_type& val)Sets val as the value for all the elements in the array object.

Example:
array<int,5> v;
//v:{10,10,10,10,10}
v.fill(10);





No comments:

Post a Comment