C++ Program to Implement Vector in STL

This C++ Program demonstrates implementation of Vector in Stl.

Here is source code of the C++ Program to demonstrate Vector in Stl. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Vector in Stl
  3.  */
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include <cstdlib>
  8. using namespace std;
  9. int main()
  10. {
  11.     vector<int> ss;
  12.     vector<int>::iterator it;
  13.     int choice, item;
  14.     while (1)
  15.     {
  16.         cout<<"\n---------------------"<<endl;
  17.         cout<<"Vector Implementation in Stl"<<endl;
  18.         cout<<"\n---------------------"<<endl;
  19.         cout<<"1.Insert Element into the Vector"<<endl;
  20.         cout<<"2.Delete Last Element of the Vector"<<endl;
  21.         cout<<"3.Size of the Vector"<<endl;
  22.         cout<<"4.Display by Index"<<endl;
  23.         cout<<"5.Dislplay by Iterator"<<endl;
  24.         cout<<"6.Clear the Vector"<<endl;
  25.         cout<<"7.Exit"<<endl;
  26.         cout<<"Enter your Choice: ";
  27.         cin>>choice;
  28.         switch(choice)
  29.         {
  30.         case 1:
  31.             cout<<"Enter value to be inserted: ";
  32.             cin>>item;
  33.             ss.push_back(item);
  34.             break;
  35.         case 2:
  36.             cout<<"Delete Last Element Inserted:"<<endl;
  37.             ss.pop_back();
  38.             break;
  39.         case 3:
  40.             cout<<"Size of Vector: ";
  41.             cout<<ss.size()<<endl;
  42.             break;
  43.         case 4:
  44.             cout<<"Displaying Vector by Index: ";
  45.             for (int i = 0; i < ss.size(); i++)
  46.             {
  47.                 cout<<ss[i]<<" ";
  48.             }
  49.             cout<<endl;
  50.             break;
  51.         case 5:
  52.             cout<<"Displaying Vector by Iterator: ";
  53.             for (it = ss.begin(); it != ss.end(); it++)
  54.             {
  55.                 cout<<*it<<" ";
  56.             }
  57.             cout<<endl;
  58.             break;
  59.         case 6:
  60.             ss.clear();
  61.             cout<<"Vector Cleared"<<endl;
  62.             break;
  63.         case 7:
  64.             exit(1);
  65.             break;
  66.         default:
  67.             cout<<"Wrong Choice"<<endl;
  68.         }
  69.     }
  70.     return 0;
  71. }

$ g++ vector.cpp
$ a.out
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 4
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 6
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 3
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 8
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 9
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 1
Enter value to be inserted: 2
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 3
Size of Vector: 6
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 4
Displaying Vector by Index: 4 6 3 8 9 2 
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 2
Delete Last Element Inserted:
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 3
Size of Vector: 5
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 5
Displaying Vector by Iterator: 4 6 3 8 9 
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 4
Displaying Vector by Index: 4 6 3 8 9 
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 6
Vector Cleared
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 3
Size of Vector: 0
 
---------------------
Vector Implementation in Stl
 
---------------------
1.Insert Element into the Vector
2.Delete Last Element of the Vector
3.Size of the Vector
4.Display by Index
5.Dislplay by Iterator
6.Clear the Vector
7.Exit
Enter your Choice: 7
 
 
------------------
(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.