This C++ Program shows implementation of Vector in Stl.
this code of the C++ Program shows Vector in Stl. The C++ program is successfully compiled and run on vc code. output is also shown below.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// write a code to insert element in vector | |
#include<vector> | |
#include<iterator> | |
#include<iostream> | |
#include <stdlib.h> | |
using namespace std; | |
int main() | |
{ | |
vector<int> myvec; // vector declar | |
vector<int>::iterator it;// iterator we use in showing element | |
int n=1,val; | |
cout<<"Enter number of element in your vector or 0 or quit :"; | |
cin>>n; | |
{ | |
for(int i =1;i<=n;i++)// for loop for pushing value inside vector | |
{ | |
cout<<"Enter your "<<i <<" elelemnt of vector :"; | |
cin>>val; | |
myvec.push_back(val);// pushing value | |
} | |
cout <<"your Vector size is :"<<myvec.size()<<endl; | |
// Display element | |
char ans; | |
cout<<"You want to display vector element (Y) ,(N) :"<<endl; | |
cin>>ans; | |
if(ans=='Y'|| ans =='y' ) | |
{ | |
for(it=myvec.begin();it!=myvec.end();++it)// iterator loop to show value. iterator always return pointer | |
{ | |
cout<<*it<<" "; | |
} | |
cout<<endl; | |
} | |
char ele; | |
cout<<"do you want to delele all element enter (y) or (n) :"; | |
cin>>ele; | |
if(ele=='Y'|| ele =='y' ) | |
{ | |
cout<<"are you sure enter (y) or (n) : "; | |
char ele; | |
cin>>ele; | |
if(ele=='Y'|| ele =='y' ) | |
{ | |
myvec.clear();// this clear all element in vector | |
} | |
} | |
else | |
{ | |
int x=0; | |
cout<<"Your vector is not deleted :"<<endl; | |
exit(x); | |
} | |
} | |
return 0; | |
} |
Enter number of element in your vector or 0 or quit :5 Enter your 1 elelemnt of vector :1 Enter your 2 elelemnt of vector :2 Enter your 3 elelemnt of vector :3 Enter your 4 elelemnt of vector :4 Enter your 5 elelemnt of vector :5 your Vector size is :5 You want to display vector element (Y) ,(N) : y 1 2 3 4 5 do you want to delele all element enter (y) or (n) :y are you sure enter (y) or (n) : y
0 Comments
If you have any doubt and suggestion Please let me know