please explain this.
For(vector<int>::iterator it = vect.begin();it!= vect.end();++it) { cout<< *it <<" "; }
Hi @abhay170, what we do is:
with
vector::iterator it
make an iterator with name it which will iterate on vectors of int only.
with
vector::iterator it = vect.begin()
we make the it iterator to point to the beginning point/cell of vector vect.
with
it!= vect.end()
we check wether it is pointing to end of vector or not
with
++it
we increment the value of it. It moves one next to the current cell it is on in memory. Same as it was happening in case on pointers. When we did ptr++.
with
cout<< *it <<” “;
Since it is a pointer *it will give the value which it points to. And hence cout will print it’s value.
So basically in above program we are iterating on a vector using a iterator it, one by one and printing the value at every index.
Hope you understand this
Thanks great explanation!