VOOZH about

URL: https://www.geeksforgeeks.org/cpp/stdarray-in-cpp/

⇱ STD::array in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

STD::array in C++

Last Updated : 9 Jun, 2022

The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. 
In order to utilize arrays, we need to include the array header: 
 

 #include <array> 


Let's see an example.  
 


Output: 
Sizes of arrays are
5
5
2

Initial ar1 : 3 4 5 1 2 
sorted ar1 : 1 2 3 4 5 
Filled ar2 : 10 10 10 10 10 
ar3 : a b

 

This C++ STL array is a kind of sequential container and is not used extremely in regular programming or in competitive programming but sometimes its member function provides an upper edge to it over the regular normal array that we use in our daily life. So, we are discussing some of the important member function that is used with such kind of array:

:

:                    array<object_type, arr_size> arr_name;

a) [ ] Operator : This is similar to the normal array, we use it to access the element store at index 'i' .

Ex:   


Output
G G

b) front( ) and back( ) function: These methods are used to access the first and the last element of the array directly.


Output
71 71

c) swap( ) function: This swap function is used to swap the content of the two arrays.

Ex: 


Output
77 80

d) empty( ) function: This function is used to check whether the declared STL array is empty or not, if it is empty then it returns true else false.

Ex: 


Output
false

e) at( ) function: This function is used to access the element stored at a specific location, if we try to access the element which is out of bounds of the array size then it throws an exception. 

Ex: 


Output
71 80

f) fill( ) function: This is specially used to initialize or fill all the indexes of the array with a similar value.

Ex:


Output
1 1 1 1 1 

g) size( ) or max_size( ) and sizeof( ) function: Both size( ) or max_size( ) are used to get the maximum number of indexes in the array while sizeof( ) is used to get the total size of array in bytes.


Output
10
10
40

h) : This function returns the pointer to the first element of the array object. Because elements in the array are stored in contiguous memory locations. This data( ) function return us the base address of the string/char type object.

Ex: 

 
 


Output
GeeksforGeeks


 

I) c:  go to this gfg link : Click_me


 

Comment
Article Tags:
Article Tags: