C++ Program to Implement Vector

This C++ Program demonstrates the implementation of Vector.

Here is source code of the C++ Program to demonstrate the implementation of Vector. 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
  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<<"Deleted 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: 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: 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: 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: 1
Enter value to be inserted: 1
 
---------------------
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: 4
Displaying Vector by Index: 4 5 3 2 1
 
---------------------
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 5 3 2 1
 
---------------------
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
Deleted 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: 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: 4
Displaying Vector by Index: 4 5 3 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: 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
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.