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