简体   繁体   中英

how to define and access custom iterator

I am trying to make a custom iterator and I am struggling with syntax part of how to define/declare and eventually access it. Below is my attempt, which results in below error:

Error   C2440   'initializing': cannot convert from 'int' to 'int *'

If someone can point me to right way to define/declare it - it would be great, for I believe my access method is standard.

Declaration

template <typename T>
class ddeque
{
public:
    typedef T* iterator;
    T& begin();
    T& end();
}

Definition

template<typename T>
T& ddeque<T>::begin()
{
    iterator = &(this->unified_array());
    return iterator;
}

template<typename T>
T& ddeque<T>::end()
{
    iterator = &(this->unified_array + this->size());
    return iterator;
}

Access part -- In t est.cpp file

// Comparing values from custom template with standard template

typename ddeque<T>::iterator itt = a.begin();
typename deque<T>::iterator ittg = g.begin();
while ((itt != a.end()) && (ittg != g.end())) 
{
   if (display) 
   {
     cout << *itt << " ";
   }
   ++itt;
   ++ittg;
}

PS : I have just kept relevant part of iterator - please let me know if additional code snippet is required.

In general:

how to define and access custom iterator

You should make a contaner class and an iterator class. The latter should be derived from std::iterator<> . You have to specify iterator tag, return value, ect. Depending on the iterator tag, you should be implementing the operators (increment, decrement, ect.). (Or better, implement everything you can which has a maximum of logarithmic complexity, then set best iterator tag.)

So begin, and end should be returning your iterator. Take a look at some stl container implementations, they are not so hard to read.

In your code

ou have made a typdef T* iterator; . This line makes another name for the pointer to T type. For readability, and to make it easier to change later, you should be returning iterator with begin and end, so:

iterator begin();
iterator end();

In the definition of the above functions, you cannot assign a value to the iterator type, I suggest returning the pointer (or iterator), for example in end():

return this->unified_array + this->size();

I think unified_array is a pointer, so I used it that way.

As pointed by SO member АндрейБеньковский I was returning reference instead of pointer- correct solution for definition would be ( what got it working )

Declaration:

template <typename T>
class ddeque
{
public:
    typedef T* iterator;
    iterator begin();
    iterator end();
}

Definition template T* ddeque::begin() { iterator = &(this->unified_array()); return iterator; }

template<typename T>
T* ddeque<T>::end()
{
    iterator = &(this->unified_array + this->size());
    return iterator;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM