Vector class in C++ (Part of STL)
//Use of vector template in cpp
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
cout<<v.size();//will display size of vector
v.push_back(10);//to add value to vector
v.push_back(20);
cout<<v.size();//will display size after adding values
cout<<endl<<"values of vector"<<endl;
cout<<v[0];//to display first value
cout<<v[1];
cout<<endl<<"values of vector through iterator";
vector<int>::iterator it = v.begin();
while( it != v.end()) {
cout <<endl<<"value of it = " << *it;
it++;
}//end of while
v.push_back(30);
v.push_back(40);
cout<<endl<<"now the size of vector is "<<v.size();
cout<<endl<<"Now values of vector are:";
for(int i=0;i<v.size();i++)
cout<<endl<<v[i];
/*so the conclusion of vector is, it is a class
that can be used to store values with in-built feature to
expand the size of vector itself.So it is implementing the
data structures who can expand at run time.
*/
}//end of main
OUTPUT :
02
values of vector
1020
values of vector through iterator
value of it = 10
value of it = 20
now the size of vector is 4
Now values of vector are:
10
20
30
40
Comments
Post a Comment