Array Container in STL in C++














































Array Container in STL in C++



Basic operations performed on the array created using array container class.
Program:

#include<iostream>
#include<array>
using namespace std; int main()
{
//Creating and Initializing the object of array class.Below line would create static array
array<int,6> arr={1,2,3,4,5,6};
//object_name.at(index) is a function,used to access elements of array.
cout<<arr.at(3)<<endl;
//Following line would throw out_of_range exception because index is greater than the size of array(if it is not commented )
// cout<<arr.at(10);
//Following is another method to access array elements
cout<<arr[0]<<endl;
//Following line would print the first element of array
cout<<arr.front()<<endl;
//Following line would print last element of array
cout<<arr.back()<<endl;
//Following line would print the size of array
cout<<arr.size()<<endl;
cout<<"Creating another array"<<endl;
array<int ,6> arr1={1,2,3};//this would automatically initialize the last 3 elements of array to 0
cout<<arr1.size()<<endl;//this would still give output as 6 as size() function returns the maximum elements a array can store
cout<<arr1.back();//this would give 0 as last element of array
return 0; }
}

OUTPUT:
4
1
1
6
6
Creating another array
6
0




Comments