C ++: How to write const_iterator?

I wrote my own container template with an iterator. How to 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: ... 
+7
c ++ iterator const-iterator
source share
3 answers

The only difference is that when you cancel the reference to a constant iterator, you get a reference to const, not a reference to an object in the container.

+4
source share

I find the easiest way to implement boost :: iterator iterators . If you want to knock over your own, I think the signature should be:

 class const_iterator : public std::iterator<std::bidirectional_iterator_tag, const T> { 

with the implementation of the same (assuming you use reference_type, etc. in your function signatures)

+2
source share

Roger Pate, value_types are "simple." I suspect you'll see const if you look at iterator_traits :: const_iterator> :: reference, which I think will be "const int &".

0
source share

All Articles