为c ++ std容器实现自定义迭代器 [英] Implement custom iterator for c++ std container

查看:149
本文介绍了为c ++ std容器实现自定义迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的Vector / Matrix类编写自定义迭代器。迭代器的目的是迭代std :: vector并跳过每个第n个元素。我已经问过类似的问题 c ++实现自定义矩阵类的迭代器确实提供了解决问题的方法。

I am trying to write a custom iterator for my Vector/Matrix class. The purpose of the iterator is to iterate through a std::vector and skip every n'th element. I already asked a similar question c++ implementing iterators for custom matrix class which indeed provides a solution to the problem.

然而这种方法太慢了。我做了很多阅读:有效的C ++,C ++ Primer等和在线资源: http://www.drdobbs.com/stl-generic-programming-writing-your-ow/184401417 如何正确实现自定义迭代器和const_iterators?当然: http://www.cplusplus.com/reference/iterator/iterator/

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? and of course: http://www.cplusplus.com/reference/iterator/iterator/.

最后一个特别提供了一个工作代码。但是我无法弄清楚代码的作用以及如何为std :: vector实现它。以下是代码:

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. 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; }

};

和主要:

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;
    // }

}

上半部分主函数返回:
$ ./main
1 2 3 4 5

The upper part in the main function returns: $ ./main 1 2 3 4 5

和下半部分:

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?

非常感谢您的评论。

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.

这篇关于为c ++ std容器实现自定义迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆