The array is a collection of elements of the same data type stored in continuous memory locations. C++ standard library contains many libraries that support the functioning of arrays.
Syntax :
array < object_type , size > array_name ;
The program demonstrates the use of the STL array container in C++. First, the program defines two arrays a and b of size 5, initialized with some values using curly braces.
array < int , 5 > a = { 10,20,30,40,50 } ;
array < int , 5 > b = { 100,200,300,400,500 } ;
The program then uses various member functions of the array container to manipulate and display its contents.
#include<iostream> #include<array> using namespace std; //STL-Array in C++ int main() { array <int,5> a= {10,20,30,40,50}; array <int,5> b= {100,200,300,400,500}; cout<<"Array Size : "<<a.size()<<endl; for(int x : a) { cout<<" "<<x; } cout<<endl; cout<<"Array Element At 3 Index : "<<a.at(3)<<endl; cout<<"Array Empty or Not : "<<a.empty()<<endl; cout<<"Array First Element: "<<a.front()<<endl; cout<<"Array Last Element: "<<a.back()<<endl; cout<<"Before Swap : "<<endl; cout<<"A : "; for(int x : a){cout<<" "<<x;} cout<<endl; cout<<"B : "; for(int x : b){cout<<" "<<x;} cout<<endl; a.swap(b); cout<<"After Swap : "<<endl; cout<<"A : "; for(int x : a){cout<<" "<<x;} cout<<endl; cout<<"B : "; for(int x : b){cout<<" "<<x;} cout<<endl; array <int,5> c; c.fill(10); cout<<"C : "; for(int x : c){cout<<" "<<x;} cout<<endl; return 0; }
Array Size : 5 10 20 30 40 50 Array Element At 3 Index : 40 Array Empty or Not : 0 Array First Element: 10 Array Last Element: 50 Before Swap : A : 10 20 30 40 50 B : 100 200 300 400 500 After Swap : A : 100 200 300 400 500 B : 10 20 30 40 50 C : 10 10 10 10 10To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions