vendredi 19 avril 2019

Iterating over const_iterator

ALL,

I have a function with the following signature:

void foo(const std::vector<Bar *> &myvec);

Inside this function I need to loop thru the members of the vector and perform some operations.

So, I tried this:

for( std::vector<Bar *>::const_iterator it = myvec.begin(); it < myvec.end(); ++it )
{
}

however this code asserts since the iterator is constant.

Now obviously the vector is constant, which means that the function shouldn't be modifying myvec.

What's the best solution here?

Can I use const_cast here to remove constness? It would be kind of hack-ish, but if it works.

But I feel there must be a better solution.

TIA!!

Aucun commentaire:

Enregistrer un commentaire