STD :: Array in C++

STD :: Array in C++

1. Linear collection of similar elements is called array (or collection of homogeneous elements is called array).

2. The array is a container used for constant size arrays. 

3. Array container in STL provides you the implementation of static array.

4. It is somewhat used in competitive programming because of its static nature.

5. It provides some member functions and some non-member functions that you should know.

6. You need to include the array header to use it.
#include<array>

Syntax of Array Container

array <object_Type, array_Size> array_Name;

For Example:
#include<array>
int main(){
 array <int, 5> even_numbers = {2, 4, 6, 8, 10};
}
The above code will create an array named even_numbers with 2, 4, 6, 8 and 10 as elements inside the array. 

The initialization of array with curly brackets { } is only possible in C++ language.


Member Functions of Array Container

Following are the important and most used member functions of array template in C++ STL.

1. at()
2. [] operator
3. front()
4. back()
5. fill()
6. swap()
7. size()
8. max_size()
9. empty()
10. begin()
11. end()


at() in Array: This method returns the value in the array at the given range. If the given range is greater than the array size, out_of_range exception is thrown.

[] in Array: The use of [] operator is same as it was for normal arrays.

front() in Array: Front() method simply returns the first element in the array.

back() in Array: Back() method returns the last element in the array.

fill() in Array: Fill() method assigns the given value to every element of the array.

swap() in Array: Swap() method swaps the content of two arrays of same type of same size.
It swaps index wise, thus element of index i of first array will be swapped with the element of index i of the second array.
For example: firstArray.swap(secondArray);

size() in Array: Size() method returns the number of elements present in the array.

max_size(): Max_size() method returns the maximum size of the array.

empty(): Empty() method can be used to check whether the array is empty or not. 

For Example: arrayName.empty() returns TRUE if the array is empty otherwise it returns FALSE.

begin() in Array: Begin() method returns the iterator pointing to the first element of the array.

end in Array(): End() method returns an iterator pointing to the element next to the last element in the array.


Example: at in array in C++
#include<iostream>
#include<array>
using namespace std;

int main(){
 array<int,10> Arr1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
   
 cout << "Value at index 1: " <<Arr1.at(1) << endl;
 cout << "Value at index 3: " <<Arr1.at(3) << endl;
 cout << "Value at index 5: " <<Arr1.at(5) << endl;
 cout << "Value at index 7: " <<Arr1.at(7) << endl;
}
Output:
Value at index 1: 1
Value at index 3: 3
Value at index 5: 5
Value at index 7: 7

Example: fill in array in C++
#include<iostream>
#include<array>
using namespace std;

int main(){
 array <float, 11> myArray;
 
 myArray.fill(101);
 
 for (int i=0; i<myArray.size(); i++){
  cout << myArray[i] << endl;
 }
}
Output:
101
101
101
101
101
101
101
101
101
101
101

Example: swap function in array in C++
#include<iostream>
#include<array>
using namespace std;

int main(){
 array <int, 5> Array1 = {5, 85, 24, 5, 9};
 array <int, 5> Array2 = {54, 32, 5, 65, 2};
 
 Array1.swap(Array2);
 
 cout << "Array1 is: ";
 for (int i=0; i<=4; i++){
  cout << Array1[i] << " ";
  }
 cout << endl;
 
 cout << "Array2 is: ";
 for (int i=0; i<=4; i++){
  cout << Array2[i] << " ";
  }
 cout << endl;
}
Output:
Array1 is: 54 32 5 65 2 
Array2 is: 5 85 24 5 9

Example: Member Functions of Array in C++
#include<iostream>
#include<array>
using namespace std;

int main(){
 array <int, 5> Array1 = {11, 22, 33, 44, 55};
 array <int, 5> Array2 = {1, 2, 3, 4, 5};
 array <int, 4> Array3 = {1, 12, 53, 64};
 
 cout << Array3.at(2) << endl;
 cout << Array3[3] << endl;
 cout << Array1.front() << endl;
 cout << Array2.back() << endl;
 Array3.fill(35);
 
 for (int i=0; i<=3; i++){
  cout << Array3[i] << " ";
  }
 cout << endl;
 
 Array1.swap(Array2);
 
 for (int i=0; i<=4; i++){
  cout << Array1[i] << " ";
  }
 cout << endl;
  
 for (int i=0; i<=4; i++){
  cout << Array2[i] << " ";
  }
 cout << endl;

 cout << "Size of Array3 is: " << Array3.size();

 return 0;
}
Output:
53
64
11
5
35 35 35 35 
1 2 3 4 5 
11 22 33 44 55 
Size of Array3 is: 4


<< Previous                                                                                      Next >>
Previous
Next Post »