Question

I am writing a function that can't be declared const because it uses an iterator on one of its attributes, and I don't know how to extract a const iterator out of a non const vector without using cbegin() and cend(). My compiler doesn't recognize this functions.

Thanks for your help.

Était-ce utile?

La solution

If you’re declaring the function const then your vector member will also be const, and begin() and end() will consequently return const_iterators. So you’re fine.

But in general you can always force a const_iterator by casting a std::vector<T> instance to std::vector<T> const&.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top