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. }

advertisement
$ 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
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn