lundi 13 avril 2020

how to traversing through vector using iterator and accessing element value?

I started to use iterators for traversing through a vector. Usually, I would use this to go through a vector:

for( int i=0; i< vector.size(); i++){
    cout<<vector[i];
}

I learned if I want to use iterators I would have to do something like this:

vector<int>::iterator col;
for(col = vector.begin(); col != vector.end(); col++){
    cout << *col;
}

But, if I want to access the value at which the iterator is pointing, how would I do that?

I tried to do this:

 int temp = *col;

but this gives an error

Assigning to 'int' from incompatible type 'std::__1::vector<int, std::__1::allocator<int> >'

I am trying this because I have a 2d vector and I'm trying to find the sum of individual columns.

Aucun commentaire:

Enregistrer un commentaire