STL-Array in C++ Programming


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.

  • The size() function is used to determine the size of the array.
  • The range-based for loop is used to print the elements of array a.
  • The at() function is used to access the element at index 3 of array a.
  • The empty() function is used to check if the array is empty.
  • The front() and back() functions are used to access the first and last elements of the array respectively.
  • The swap() function is used to swap the contents of arrays a and b.
  • The fill() function is used to fill array c with the value 10.

Source Code

#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;
}
 

Output

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 10
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples