简体   繁体   English

如何定义和访问自定义迭代器

[英]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 访问部分-in est.cpp文件

// 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. PS:我刚刚保留了迭代器的相关部分-如果需要其他代码段,请告诉我。

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<> . 后者应从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. 看一下一些stl容器实现,它们并不难读。

In your code 在你的代码中

ou have made a typdef T* iterator; ou已经做了typdef T* iterator; . This line makes another name for the pointer to T type. 该行为指向T类型的指针起了另一个名字。 For readability, and to make it easier to change later, you should be returning iterator with begin and end, so: 为了提高可读性,并使以后更改变得更容易,您应该返回带有begin和end的迭代器,因此:

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(): 在上述函数的定义中,您不能为迭代器类型分配值,我建议返回指针(或迭代器),例如在end()中:

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

I think unified_array is a pointer, so I used it that way. 我认为unified_array是一个指针,所以我用了这种方式。

As pointed by SO member АндрейБеньковский I was returning reference instead of pointer- correct solution for definition would be ( what got it working ) 正如SO成员АндрейБеньковский所指出的那样,我返回的是引用,而不是指针的正确定义解决方案(是什么使它起作用)

Declaration: 宣言:

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

Definition template T* ddeque::begin() { iterator = &(this->unified_array()); 定义模板T * ddeque :: begin(){迭代器=&(this-> unified_array()); return iterator; 返回迭代器; } }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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