10
\$\begingroup\$

So I have written a custom iterator for std::map that allows access to just the "values" of the map and I was wondering if it is the "correct" way to implement std::iterator (acts identically to std::iterator).

Here is the implementation I have written:

template<typename K, typename V>
class MapValueIterator : public std::iterator<std::forward_iterator_tag, V> {
public:
    MapValueIterator();

    MapValueIterator(typename std::map<K, V>::iterator iterator) : iterator(iterator) { }

    MapValueIterator(const MapValueIterator<K, V> &iterator) : iterator(iterator.iterator) { }

    MapValueIterator<K, V> &operator=(const MapValueIterator<K, V> &iterator) {
        this->iterator = iterator.iterator;
    }

    bool operator==(const MapValueIterator<K, V> &iterator) const {
        return this->iterator == iterator.iterator;
    }

    bool operator!=(const MapValueIterator<K, V> &iterator) const {
        return this->iterator != iterator.iterator;
    }

    V &operator*() const {
        return (iterator->second);
    }

    V *operator->() const {
        return &(iterator->second);
    }

    MapValueIterator<K, V> &operator++() {
        ++iterator;

        return *this;
    }

    MapValueIterator<K, V> operator++(int) {
        return (*this)++;
    }

private:
    typename std::map<K, V>::iterator iterator;
};

And an example of how it can be used:

std::map<int, int> values;

values.insert(std::make_pair(0, 1));
values.insert(std::make_pair(1, 2));
values.insert(std::make_pair(2, 5));

MapValueIterator<int, int> begin(values.begin());
MapValueIterator<int, int> end(values.end());

for (auto iterator = begin; iterator != end; ++iterator) {
    printf("%d\n", *iterator);
}
\$\endgroup\$
4
  • \$\begingroup\$ Nice, but you could also template on the map type to be able to use it with an unordered_map or a multimap. You could arguably use a template-template parameter in this case. \$\endgroup\$
    – glampert
    Mar 7, 2016 at 4:12
  • \$\begingroup\$ @glampert, I have looked into doing this, however it seems unordered_map and map have differing numbers of template parameters and all of the parameters are required when defining the template (i.e. template<template<class, class, class, class> class M, typename K, typename V> only works for map while template<template<class, class, class, class, class> class M, typename K, typename V> only works for unordered_map (note the extra class in the template). \$\endgroup\$ Mar 7, 2016 at 12:19
  • 2
    \$\begingroup\$ As it turns out, I can use template<template<class...> M, typename K, typename V> which works perfectly! \$\endgroup\$ Mar 7, 2016 at 13:33
  • \$\begingroup\$ You don't need template/template parameters. You can just use the template M. Then the constructor parameter is M::iterator and the value type can be retrieved from M::mapped_type. \$\endgroup\$ Mar 7, 2016 at 20:56

2 Answers 2

8
\$\begingroup\$

It looks about perfect.

You got the post increment wrong though:

MapValueIterator<K, V> operator++(int) {
    return (*this)++;
}

I believe this goes into an infinite recursion.

Thus this is normally implemented as:

MapValueIterator<K, V> operator++(int) {
    MapValueIterator<K, V>  result(*this);  // get a copy for return
                                            // so this can be used
                                            // unaltered in the expression

    // Now implement the current object.
    ++(*this);

    // Returned the saved copy.
    return result;
}

The only difference is that the std::map<K,V>::iterator type actually implements the Bidirectional iterator concept. The question is why is your iterator only implement the Forward iterator concept.

\$\endgroup\$
8
\$\begingroup\$

I have one other addition to what Loki has already pointed out.

The C++ committee has voted to deprecate inheriting from std::iterator as of C++17. The current recommendation is apparently to write those typedefs yourself.

template<typename K, typename V>
class MapValueIterator {
public:
    typedef V value_type;
    typedef std::ptrdiff_t difference_type;
    typedef V *pointer;
    typedef V &reference;
    typedef std::ForwardIteratorTag iterator_category;

// ...

Feel free to read the proposal to deprecate it, if you care about why they're doing it (and such).

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Just to clarify something for future readers. std::iterator is not getting deprecated, only the practice of inheriting from std::iterator in your custom iterator class has been deprecated. \$\endgroup\$
    – KeyC0de
    Oct 2, 2019 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.