This C++ Program demonstrates implementation of Array in STL.
Here is source code of the C++ Program to demonstrate Array in STL. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below. Compilable only in C++ 11.
/*
* C++ Program to Implement Array in Stl
*/
#include <iostream>
#include <array>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
array<int, 5> arr;
array<int, 5>::iterator it;
int choice, item;
arr.fill(0);
int count = 0;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"Array Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the Array"<<endl;
cout<<"2.Size of the array"<<endl;
cout<<"3.Front Element of Array"<<endl;
cout<<"4.Back Element of Array"<<endl;
cout<<"5.Display elements of the Array"<<endl;
cout<<"6.Exit"<<endl;
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<<arr.size()<<endl;
break;
case 3:
cout<<"Front Element of the Array: ";
cout<<arr.front()<<endl;
break;
case 4:
cout<<"Back Element of the Stack: ";
cout<<arr.back()<<endl;
break;
case 5:
for (it = arr.begin(); it != arr.end(); ++it )
cout <<" "<< *it;
cout<<endl;
break;
case 6:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
$ g++ array.cpp $ a.out --------------------- 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
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Practice Programming MCQs
- Check Data Structure Books
- Check Programming Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship