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.

Was it helpful?

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&.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top