Data Structures - Vector
Vector
v.begin() - gives the pointer to the beginning of the vector;
v.end() - gives the pointer to the last location + 1
There are many other such functions but these are the most basic ones that are usually used.
A vector is a dynamic array brought to you by C++ stl to ease your task.
In almost every other program , you need an arrya and most of the times you dont know what its size will be .
So instead of wasting memory creating the largest size of array we can use a dynamically created array with the size that is required at that point of time . But implementing this on our own can be a little tough.
This problem is solved by the standard template library of C++.
We can initialise a vector of type integer as
#include<vector>
int main()
{
vector<int> v1;
}
or we can initialise it as vector<int> v;
v.assign(5,10)
This will create 5 blocks and fill them with value 10.
Or we can insert a value in this vector by using the command v.push_back(value);
To print the vector or for any operation we can do it just like an array;
for(int i=0;i<v.size();i++)
{
cout<<v[i]<<" ";
}
v.size() - gives the number of elements in the vector;v.begin() - gives the pointer to the beginning of the vector;
v.end() - gives the pointer to the last location + 1
There are many other such functions but these are the most basic ones that are usually used.
Comments
Post a Comment