iteratorstdvectorboost-iterators

Iterating over keys/values of a std::vector and other containers in C++ (MWE)


I base my code below on the following post which is really usefull. Iterating over keys/values The thing works for std::maps (ValueGetter needs adjusting) but why does the following code not work for std::vector as well? I put the code in a class SpecialList which I then can inherit from different containers... The problem lies in accessing (*it) at the end. I dont understand why this does not compile? (MWE)

#include <functional>
#include <map>
#include <boost/iterator/transform_iterator.hpp>


class A{
public:
    A(int i):a(i){}
    int a;
};

template <typename T>
struct ValueGetterVec : std::unary_function<typename  std::vector<T>::iterator::value_type,
                                            typename std::vector<T>::iterator::value_type>
{
    const typename std::vector<T>::iterator::value_type& operator()
     (const typename std::vector<T>::iterator::value_type & p) const
    { return p;}
};

class SpecialList : public std::vector< A* > {
    public:

    typedef boost::transform_iterator< ValueGetterVec<iterator>, iterator>     
    value_iterator;

    value_iterator beginValueIterator()
    {
        return value_iterator(this->begin(), ValueGetterVec<iterator>() );
    }

    value_iterator endValueIterator()
    {
        return value_iterator(this->end(), ValueGetterVec<iterator>() );
    }

};

SpecialList list;
list2.push_back(new A(1));
list2.push_back(new A(2));
 {
    SpecialList::value_iterator it;

    for(it = list.beginValueIterator(); it != list.endValueIterator(); it++){
        std::cout << (*it)->a << std::endl;
    }
}

Solution

  • Found the ERROR: Replace ValueGetterVec<iterator> ==> ValueGetterVec<A*>