Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

c++ - Generic iterator

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

with the appropriate operators overloaded.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...