سؤال

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.

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top