<p>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.</p>

<p>The custom list defines an iterator;</p>

<pre><code>class Iterator: public std::iterator&lt;std::forward_iterator_tag, T&gt; {
    // ...
}

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

Iterator end() {
    return (Iterator(NULL));
}
</code></pre>

<p>with the appropriate operators overloaded. </p>

<p>Ideally, I would like to do this;</p>

<pre><code>class Foo {
public:
    Foo() {
        std::list&lt;int&gt; x;
        std::vector&lt;int&gt; y;
        custom_list&lt;int&gt; z;

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

        // ...
    };
private:
    std::iterator&lt;int&gt; iter;
};
</code></pre>

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

<p>Is there an elegant way to solve this problem?</p>

[{'answer_id': 9972, 'author': 'David Sykes', 'author_id': 259, 'author_profile': 'https://Stackoverflow.com/users/259', 'pm_score': 5, 'selected': True, 'text': '<p>Here are some articles you might find of interest</p>\n\n<p><a href="http://accu.org/index.php/journals/479" rel="noreferrer">Giving STL Iterators a Base Class</a></p>\n\n<p><a href="http://thbecker.net/free_software_utilities/type_erasure_for_cpp_iterators/start_page.html" rel="noreferrer">Type Erasure for C++ Iterators</a></p>\n\n<p><a href="http://stlab.adobe.com/classadobe_1_1any__iterator.html" rel="noreferrer">any_iterator Class Reference</a></p>\n'}, {'answer_id': 10895, 'author': 'graham.reeds', 'author_id': 342, 'author_profile': 'https://Stackoverflow.com/users/342', 'pm_score': 1, 'selected': False, 'text': '<p>Better late than never...</p>\n\n<p>The latest issue of <a href="http://accu.org/index.php/aboutus/aboutjournals" rel="nofollow noreferrer">C-Vu</a> turned up and guess what was in it: That\'s right, iterators that do <em>exactly</em> what you wanted.</p>\n\n<p>Unfortunately you need to become a member of the <a href="http://accu.org/" rel="nofollow noreferrer">ACCU</a> to view the magazine (the article references the Overload article from 2000 that David links to). But for a measly price a year you get a nice magazine to read, conferences and user groups. When you become a member you can view PDF\'s of the back issues so <a href="http://accu.org/index.php/joining" rel="nofollow noreferrer">what are you waiting for</a>?</p>\n'}, {'answer_id': 127971, 'author': 'MSalters', 'author_id': 15416, 'author_profile': 'https://Stackoverflow.com/users/15416', 'pm_score': 1, 'selected': False, 'text': '<p>A case of being careful what you ask for. The any_iterator classes you see work on an unbounded set of iterator types. You only have three, which you know up front. Sure, you might need to add a fourth type in the future, but so what if that takes O(1) extra lines of code ?</p>\n\n<p>The big advantage of a closed set of possible contained types is that you have an upper bound on sizeof(), which means you can avoid the heap and the indirection it brings. Basically, stuff them all in a boost::variant and call apply_visitor.</p>\n'}]
['https://Stackoverflow.com/questions/9938', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/263/']