All about array STL in c++ for competitive programming( initialization, traversal and methods/functions ) :

array STL

Important Points:

  1. It is a sequence container that encapsulates fixed size array.
  2. Size is needed at compile time.
  3. It's rarely used in competitive programming as vector is there which is far more better than array STL.
  4. Remember to use the header file #include<array> before using array STL.

Advantages of using vector over array:

  • Vector is dynamic in nature so, size automatically increases with insertion of elements. 
  • Array is fixed size, once initialized can't be resized but vector can. 
  • Reserve space can be given for vector, whereas for arrays you cannot give reserved space. 
  • Vectors can store any type of objects, whereas an array can store only homogeneous values. 
  • Multiple objects can be stored in vector. 
  • Elements can be deleted in vector. 
 

Disadvantages of vector over array:

  • Vector occupies more memory.
  • Vector takes more time in accessing elements.
  • Array is more appropriate for storing a fixed no. of elements.
  • Array support efficient random access to the members.
  • It is easy to sort an array.

 

 

Different ways to initialize a array STL

1. The following ways are used to initialize an array STL:  

       array < int, 5 > a ;
       a = {1, 3, 4, 5, 2} ; 
 
                or
 
     
array < int, 5 > a = {1, 3, 4, 5, 2} ;
 
 
2. filling an array with the same specified element :
Suppose we want an array of size 3 and it's every element is 10
Therefore,
 
    array < int, 3 > a ;
    a.fill( 3 ) ; 
 
 
 

 

All array STL methods/ functions  

1. array STL important iterator functions:

Iterator definition: An iterator is something which helps in moving within the container. It points to a specific element and it moves according to our commands from one element to another.
 
1. begin( ) : returns an iterator pointing to the first element of the array.
 
2. end( )returns an iterator pointing to the theoretical element after the last element of the array.

Other functions:
  •     rbegin( )
  •     cbegin( )
  •     crbegin( )
  •     rend( )
  •     cend( )
  •     crend( )
 
 

2. array STL  important capacity related functions:

1. size( ) : returns the number of elements in the array.

2. empty( ) : returns true if the container is empty otherwise, false.

Other functions:
  •     max_size( )
 
 

3. array STL important element access functions:

1. operator [ ] or at( ) : returns the element at the specified index number in brackets.
 
2. front( )returns the first element of the array.

3. back( ) : returns the last element of the array


Other functions:
  •     data( )
 
 
 

4. array STL important modifying functions or modifiers:

1. fill( ) : fills the array with the element specified in the parenthesis.
 
2. swap( ) : used to exchange the content of two arrays of a same type (size may differ).
 
3. sort( ) : used to sort the array.

 
 
 
 

 

List of all array STL important functions with example

 1. Initializing array : array< int, 3 > a ;
 
 2. Putting elements into it : 
      a[ 0 ] = 10 ;
      a[ 1 ] = 30 ;
      a[ 2 ] = 20 ; 
     Therefore, 
     a = { 10, 30, 20 } 
 
3. cout << a.size( ) ;               // 3
 
4. Checking whether array is empty or not :
    if( a.empty( ) = = false )
    cout << "not empty" ;     //it will be printed
    else
    cout << "empty" ;
 
5. cout << a[1] ;     // 30
 
6. cout << a.at(1) ;     // 30  
 
7. cout << a.front( ) ;     // 10 
 
8. cout << a.back( ) ;   // 20
 
9. sort( a.begin( ), a.end( ) ) ;  
      Therefore, 
       a = { 10, 20, 30 }
 




array STL traversal methods

Let our array be a :
array < int, n > a ;
 
1. for ( auto &i : a)
    {
          cout << i << " "; 
    }

       
2. for ( auto i = a.begin( ) ; i ! = a.end( ) ; i++)
    {
         cout << *i << " " ;
    }
 
  
3. declaring an iterator it :
    array < int, n > : : it ;
    for ( it = a.begin( ) ; it!=a.end( ) ; it++)
    {
         cout << *it << " " ;
    }
 
4. for ( int i = 0 ; i < a.size( ); i++ )
    {
         cout << a[ i ] << " " ;
    } 
 
 
 
 

 

 

Hi reader! got any queries or need more explanation for something? Feel free to comment below.

Comments

Popular posts from this blog

Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits, Maximum Pairwise Product ) :

HackerEarth's basic programming solutions( Seating Arrangement, Zoos, Anagrams ) :

HackerEarth's basic programming solutions( Minimize Cost, Magical Word, Best Index ) :