Friday 24 November 2017

C++ Program to Implement Array in STL


Code:

#include    iostream
#include    array
#include    string
#include    cstdlib
using namespace std;
int main()
{
    array arr;
    array::iterator it;
    int choice, item;
    arr.fill(0);
    int count = 0;
    while (1)
    {
        cout<<"\n---------------------"<
        cout<<"Array Implementation in Stl"<
        cout<<"\n---------------------"<
        cout<<"1.Insert Element into the Array"<
        cout<<"2.Size of the array"<
        cout<<"3.Front Element of Array"<
        cout<<"4.Back Element of Array"<
        cout<<"5.Display elements of the Array"<
        cout<<"6.Exit"<
        cout<<"Enter your Choice: ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Enter value to be inserted: ";
            cin>>item;
            arr.at(count) = item;
            count++;
            break;
        case 2:
            cout<<"Size of the Array: ";
            cout<
            break;
        case 3:
            cout<<"Front Element of the Array: ";
            cout<
            break;
        case 4:
            cout<<"Back Element of the Stack: ";
            cout<
            break;
        case 5:
            for (it = arr.begin(); it != arr.end(); ++it )
                cout <<" "<< *it;
            cout<
            break;
        case 6:
            exit(1);
            break;
        default:
            cout<<"Wrong Choice"<
        }
    }
    return 0;
}


Output:

---------------------
Array Implementation in Stl

---------------------
1.Insert Element into the Array
2.Size of the array
3.Front Element of Array
4.Back Element of Array
5.Display elements of the Array
6.Exit
Enter your Choice: 1
Enter value to be inserted: 2
---------------------
Array Implementation in Stl

---------------------
1.Insert Element into the Array
2.Size of the array
3.Front Element of Array
4.Back Element of Array
5.Display elements of the Array
6.Exit
Enter your Choice: 1
Enter value to be inserted: 3
---------------------
Array Implementation in Stl

---------------------
1.Insert Element into the Array
2.Size of the array
3.Front Element of Array
4.Back Element of Array
5.Display elements of the Array
6.Exit
Enter your Choice: 1
Enter value to be inserted: 4
---------------------
Array Implementation in Stl

---------------------
1.Insert Element into the Array
2.Size of the array
3.Front Element of Array
4.Back Element of Array
5.Display elements of the Array
6.Exit
Enter your Choice: 1
Enter value to be inserted: 5
---------------------
Array Implementation in Stl

---------------------
1.Insert Element into the Array
2.Size of the array
3.Front Element of Array
4.Back Element of Array
5.Display elements of the Array
6.Exit
Enter your Choice: 1
Enter value to be inserted: 6
---------------------
Array Implementation in Stl

---------------------
1.Insert Element into the Array
2.Size of the array
3.Front Element of Array
4.Back Element of Array
5.Display elements of the Array
6.Exit
Enter your Choice: 2
Size of the Array: 5

---------------------
Array Implementation in Stl

---------------------
1.Insert Element into the Array
2.Size of the array
3.Front Element of Array
4.Back Element of Array
5.Display elements of the Array
6.Exit
Enter your Choice: 3
Front Element of the Array: 2

---------------------
Array Implementation in Stl

---------------------
1.Insert Element into the Array
2.Size of the array
3.Front Element of Array
4.Back Element of Array
5.Display elements of the Array
6.Exit
Enter your Choice: 4
Back Element of the Stack: 6

---------------------
Array Implementation in Stl

---------------------
1.Insert Element into the Array
2.Size of the array
3.Front Element of Array
4.Back Element of Array
5.Display elements of the Array
6.Exit
Enter your Choice: 5 
2 3 4 5 6

---------------------
Array Implementation in Stl

---------------------
1.Insert Element into the Array
2.Size of the array
3.Front Element of Array
4.Back Element of Array
5.Display elements of the Array
6.Exit
Enter your Choice: 6


------------------
(program exited with code: 1)
Press return to continue



More C++ Programs:


















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...