I'm writing a collection of containers and iterators for an environment where STL and Boost are off the table. I'm struggling somewhat with dealing with containers, such as a Vector, that can contain both value types and pointer types, when it comes to handling iterators and their return types - via a GetValue function, for example. It is further complicated by wanted to support const iterators. I have seen here about the definition of a series of traits: value_type, reference, pointer. My question is how are these used in the context of creating iterators? Essentially, given that I want
Object & Vector<Object>::Iterator::GetValue()
Object * Vector<Object*>::Iterator::GetValue()
const Object & Vector<Object>::ConstIterator::GetValue()
const Object * Vector<Object*>::ConstIterator::GetValue()
How do value_type, reference, pointer factor into this?
The member types of iterators aren't used for very much. Fortunately, you don't need to bother defining them. The std::iterator
base class does it for you.
template< typename value_type >
struct my_vector_iterator
: std::iterator< std::random_access_iterator_tag, value_type >
…
template< typename value_type >
struct my_vector_const_iterator
: std::iterator< std::random_access_iterator_tag, value_type const >
…
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.