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

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.