What is the difference between const_iterator and non-const iterator in the C++ STL?

Konrad picture Konrad · Nov 21, 2008 · Viewed 95.2k times · Source

What is the difference between a const_iterator and an iterator and where would you use one over the other?

Answer

Dominic Rodger picture Dominic Rodger · Nov 21, 2008

const_iterators don't allow you to change the values that they point to, regular iterators do.

As with all things in C++, always prefer const, unless there's a good reason to use regular iterators (i.e. you want to use the fact that they're not const to change the pointed-to value).