What is the difference between a const_iterator
and an iterator
and where would you use one over the other?
C++ – the difference between const_iterator and non-const iterator in the C++ STL
cconstantsiteratorstl
Related Topic
- C++ – What are the differences between a pointer variable and a reference variable in C++
- C++ – the difference between g++ and gcc
- Python – What does the “yield” keyword do
- C++ – The Definitive C++ Book Guide and List
- C++ – the difference between const int*, const int * const, and int const *
- C++ – the copy-and-swap idiom
- C++ – What are the basic rules and idioms for operator overloading
- C++ – Difference between `constexpr` and `const`
Best Answer
const_iterator
s don't allow you to change the values that they point to, regulariterator
s 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 notconst
to change the pointed-to value).