jeudi 28 décembre 2017

Difference between const vector and const iterator

If I am passing a vector to a function as reference and I want the function should not modify the vector, what is better - using const vector<> or using vector::const_iterator?

For example, I am traversing through a vector passed from main() to foo().

void foo (const vector<int> &v1) {
vector<int>::const_iterator m;

for(m=v1.begin();m1!=v1.end();++m1)
//loop body

}

And main() is

int main() {
vector<int> v11={0,1,2,3,4};
foo(v11);
}

In this case, v1 should be const or m should be const_iterator?

Aucun commentaire:

Enregistrer un commentaire