C++ Program to Implement Array in STL

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.

  1. /*
  2.  * C++ Program to Implement Array in Stl
  3.  */
  4. #include <iostream>
  5. #include <array>
  6. #include <string>
  7. #include <cstdlib>
  8. using namespace std;
  9. int main()
  10. {
  11.     array<int, 5> arr;
  12.     array<int, 5>::iterator it;
  13.     int choice, item;
  14.     arr.fill(0);
  15.     int count = 0;
  16.     while (1)
  17.     {
  18.         cout<<"\n---------------------"<<endl;
  19.         cout<<"Array Implementation in Stl"<<endl;
  20.         cout<<"\n---------------------"<<endl;
  21.         cout<<"1.Insert Element into the Array"<<endl;
  22.         cout<<"2.Size of the array"<<endl;
  23.         cout<<"3.Front Element of Array"<<endl;
  24.         cout<<"4.Back Element of Array"<<endl;
  25.         cout<<"5.Display elements of the Array"<<endl;
  26.         cout<<"6.Exit"<<endl;
  27.         cout<<"Enter your Choice: ";
  28.         cin>>choice;
  29.         switch(choice)
  30.         {
  31.         case 1:
  32.             cout<<"Enter value to be inserted: ";
  33.             cin>>item;
  34.             arr.at(count) = item;
  35.             count++;
  36.             break;
  37.         case 2:
  38.             cout<<"Size of the Array: ";
  39.             cout<<arr.size()<<endl;
  40.             break;
  41.         case 3:
  42.             cout<<"Front Element of the Array: ";
  43.             cout<<arr.front()<<endl;
  44.             break;
  45.         case 4:
  46.             cout<<"Back Element of the Stack: ";
  47.             cout<<arr.back()<<endl;
  48.             break;
  49.         case 5:
  50.             for (it = arr.begin(); it != arr.end(); ++it )
  51.                 cout <<" "<< *it;
  52.             cout<<endl;
  53.             break;
  54.         case 6:
  55.             exit(1);
  56.             break;
  57.         default:
  58.             cout<<"Wrong Choice"<<endl;
  59.         }
  60.     }
  61.     return 0;
  62. }

$ 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
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.