const-iterator

Are const_iterators faster ?

Our coding guidelines say prefer const_iterator, because they are little faster compared to normal iterator. It seems like compiler optimizes the code when you use the const _iterator. Is it really correct ? If yes, what really happens internally to make const_iterator takes the edge?. EDIT: I wrote small test to check const_iterator ...

How to remove constness of const_iterator?

As an extension to this question Are const_iterators faster?, I have another question on const_iterators. How to remove constness of a const_iterator? Though iterators are generalised form of pointers but still const_iterator and iterators are two different things. Hence, I believe, I also cannot use const_cast<> to covert from const_i...

Should I prefer iterators over const_iterators?

Someone here recently brought up the article from Scott Meyers that says: Prefer iterators over const_iterators (pdf link). Someone else was commenting that the article is probably outdated. I'm wondering what your opinions are? Here is mine: One of the main points of the article is that you cannot erase or insert on a const_iterat...

C++ : How to write a const_iterator?

I've written my own container template with an iterator. How do I implement const_iterator? template <class T> class my_container { private: ... public: my_container() : ... { } ~my_container() { } class iterator : public std::iterator<std::bidirectional_iterator_tag, T> { public: ... ...

Constant correctness

In the printMessage if you access the vector of a constant class using the index it works fine, but not with the the iterator (*itr). If the iterator is declared as constant_iterator then it works fine. Why? In both cases I am reading the data and not modifying the vector. Can someone shed some light? #include <iostream> #inclu...

C++ iterator and const_iterator problem for own container class

Hi there, I'm writing an own container class and have run into a problem I can't get my head around. Here's the bare-bone sample that shows the problem. It consists of a container class and two test classes: one test class using a std:vector which compiles nicely and the second test class which tries to use my own container class in ex...

How to correctly implement custom iterators and const_iterators ?

I have a custom container class for which I'd like to write the iterator and const_iterator classes. I never did this before and I failed to find an appropriate how-to. What are the guidelines regarding iterator creation, and what should I be aware of ? I'd also like to avoid code duplication (I feel that const_iterator and iterator sh...