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

Categories

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

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

1 Answer


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...