通用迭代器 [英] Generic iterator

查看:157
本文介绍了通用迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个通用的访问一组容器的方法。除了另一个自定义列表,我还有一个标准的向量和列表。

I am trying to find a generic way of accessing a set of containers. I have a standard vector and list in addition to another custom list.

自定义列表定义了一个迭代器;

The custom list defines an iterator;

class Iterator: public std::iterator<std::forward_iterator_tag, T> {
    // ...
}

Iterator begin() {
    return (Iterator(root));
}

Iterator end() {
    return (Iterator(NULL));
}

理想情况下,我想这样做;

Ideally, I would like to do this;

class Foo {
public:
    Foo() {
        std::list<int> x;
        std::vector<int> y;
        custom_list<int> z;

        iter = x.begin(); // OR
        iter = y.begin(); // OR
        iter = z.begin();

        // ...
    };
private:
    std::iterator<int> iter;
};

但显然这些都是不同类型的迭代器。

But obviously these are all iterators of different types. I can assume all the containers are of the same type however.

有一个优雅的方法来解决这个问题吗?

Is there an elegant way to solve this problem?

推荐答案

以下是您可能感兴趣的一些文章

Here are some articles you might find of interest

给予STL迭代器基类

键入C ++迭代器的Erasure

any_iterator类参考

这篇关于通用迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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