简体   繁体   English

为C ++ std容器实现自定义迭代器

[英]Implement custom iterator for c++ std container

I am trying to write a custom iterator for my Vector/Matrix class. 我正在尝试为Vector / Matrix类编写一个自定义迭代器。 The purpose of the iterator is to iterate through a std::vector and skip every n'th element. 迭代器的目的是迭代std :: vector并跳过第n个元素。 I already asked a similar question c++ implementing iterators for custom matrix class which indeed provides a solution to the problem. 我已经问过类似的问题,即c ++为自定义矩阵类实现迭代器 ,确实为该问题提供了解决方案。

However this approach is too slow. 但是,这种方法太慢了。 I did quite a bit of reading: Effective C++, C++ Primer etc. and on-line resources: http://www.drdobbs.com/stl-generic-programming-writing-your-ow/184401417 or How to correctly implement custom iterators and const_iterators? 我做了很多阅读:有效的C ++,C ++ Primer等以及在线资源: http : //www.drdobbs.com/stl-generic-programming-writing-your-ow/184401417如何正确实现自定义迭代器和const_iterators? and of course: http://www.cplusplus.com/reference/iterator/iterator/ . 并且当然: http : //www.cplusplus.com/reference/iterator/iterator/

The last one in particular provides a working code. 特别是最后一个提供了工作代码。 However I can't really figure out what the code does and how to implement this for a std::vector. 但是我真的不能弄清楚代码的作用以及如何为std :: vector实现这一点。 Here is the code: 这是代码:

template<class T>
class iter: public std::iterator<std::random_access_iterator_tag, T>{
private:
    T* m_pter;

public:

    iter(T* value):  m_pter(value){}
    iter(const iter& other_it): m_pter(other_it.m_pter){}
    iter& operator++() { ++m_pter; return *this; }
    bool operator!=(const iter& rhs) {return m_pter!=rhs.m_pter;}
    T& operator*() { return *m_pter; }

};

and the main: 主要:

int main(int argc, char *argv[]){

    int a[]  = {1,2,3,4,5,6};
    iter<int> from(a);
    iter<int> to(a+5);
    for (iter<int> iter = from; iter != to; ++iter) {
        std::cout << *iter << std::endl;
    }

    // std::vector<int> a  = {1,2,3,4,5,6};
    // iter<std::vector<int> > from(a);
    // iter<std::vector<int> > to(a+5);
    // for (iter<std::vectorVECTOR<int> > iter=from; iter!=to; ++iter) {
    //     std::cout << *iter << std::endl;
    // }

}

The upper part in the main function returns: $ ./main 1 2 3 4 5 main函数的上部返回:$ ./main 1 2 3 4 5

and the lower part: 下半部分:

main.cpp:49:21: error: no matching function for call to ‘iter<int>::iter(std::vector<int>&)’
     iter<int> from(a);

I would really like to understand why the code works for the array but not the vector. 我真的很想了解为什么代码适用于数组而不适用于向量。 What would I need to change in order for it to work for the vector? 我需要更改什么才能使其适用于载体?

Your comments are greatly appreciated. 非常感谢您的评论。

Vincent 文森特

That was actually quite simple and this article helped. 这实际上非常简单,并且本文有所帮助。

int main {
    std::vector<int> a  = {1,2,3,4,5,6};
    iter<int> from(&a[0]);
    iter<int> to(&a[5]);
    for (iter<int> iter = from; iter < to; ) {
        std::cout << *iter << std::endl;
    }
}

which is good enough for further specifications. 这足以满足进一步的规格要求。

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

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