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.
/** C++ Program to Implement Vector*/#include <iostream>#include <vector>#include <string>#include <cstdlib>using namespace std;
int main()
{vector<int> ss;
vector<int>::iterator it;
int choice, item;
while (1)
{cout<<"\n---------------------"<<endl;
cout<<"Vector Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the Vector"<<endl;
cout<<"2.Delete Last Element of the Vector"<<endl;
cout<<"3.Size of the Vector"<<endl;
cout<<"4.Display by Index"<<endl;
cout<<"5.Dislplay by Iterator"<<endl;
cout<<"6.Clear the Vector"<<endl;
cout<<"7.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
ss.push_back(item);
break;
case 2:
cout<<"Deleted Last Element Inserted"<<endl;
ss.pop_back();
break;
case 3:
cout<<"Size of Vector: ";
cout<<ss.size()<<endl;
break;
case 4:
cout<<"Displaying Vector by Index: ";
for (int i = 0; i < ss.size(); i++)
{cout<<ss[i]<<" ";
}cout<<endl;
break;
case 5:
cout<<"Displaying Vector by Iterator: ";
for (it = ss.begin(); it != ss.end(); it++)
{cout<<*it<<" ";
}cout<<endl;
break;
case 6:
ss.clear();
cout<<"Vector Cleared"<<endl;
break;
case 7:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}}return 0;
}
$ 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.
Related Posts:
- Practice Computer Science MCQs
- Check Programming Books
- Check Computer Science Books
- Practice Programming MCQs
- Practice Design & Analysis of Algorithms MCQ