Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ iterator for map derived class

I have a class that internally has an instance of map:

template<typename K, typename V>
class my_map {
    private:
        std::map<K, V> mmap;

Internally to the class I need to create an iterator for templated types, how can I do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To avoid confusion with typename keyword. I suggest to do the following

template<typename K, typename V>
class my_map {
private:
    std::map<K, V> mmap;
public:
    typedef typename std::map<K, V>::iterator iterator;
    typedef typename std::map<K, V>::const_iterator const_iterator;

    iterator begin() {return mmap.begin();}
    const_iterator begin() const {return mmap.begin();}
    .
    .
    .
};

You can now use it as my_map<K, V>::iterator or my_map<K, V>::const_iterator.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading