2

I want to write my own algorithm (just a function really) that takes a range of iterators. If the iterators are from a map, I want to use the data (iterator->second) value. If the iterator is "normal" like a vector or list, I just want to use the dereferenced iterator value.

4
  • 1
    I would recommend adapting the iterator instead. I posted two examples of a key iterator in an answer to another question. It would be trivial to transform either of those into a value iterator. Aug 16, 2012 at 19:22
  • I like that, but unfortunately I can't count on having boost on all the systems. Aug 16, 2012 at 19:26
  • 1
    Keep reading: the second implementation presented in that answer does not make use of Boost. Aug 16, 2012 at 19:26
  • 1
    An iterator adapator is the correct solution. Your function should not change behavior just because my value type happened to be a pair. It's the caller's job to make sure the range is correctly defined, not yours.
    – GManNickG
    Aug 16, 2012 at 23:07

5 Answers 5

5

I think, value-getter idea is right here, but you can implement it without c++11 and without structs at all, only using functions:

template <typename T> 
const T& get(const T& t)
{
    return t;
}

template <typename T, typename V>
const V& get(const std::pair<T,V>& t)
{
    return t.second;
}


int main()
{
    std::vector<int> v = {1};
    std::cout << get(*v.begin());

    std::cout << "\n----\n";

    std::map<int, std::string> m;
    m.insert(std::make_pair(0, "sss"));
    std::cout << get(*m.cbegin());
}
1
  • 1
    I really like this. It's simple, reduces the amount of code that's specialized, and will compile on the old collection of compilers I'm stuck with. Aug 16, 2012 at 19:33
4

You can make a value-getter class that extracts the value you're interested in. Note that this approach doesn't work if you store pairs in any container (it transforms all pairs, whether in a map or not). I'd think it would be a much clearer approach to only accept "regular" iterators and let it be callers job to transform map iterators appropriately (as suggested in the comments to your question.)

template<typename T>
struct get {
  static auto val(const T& t) -> const T&
  {
    return t;
  }
};

template<typename U, typename V>
struct get<std::pair<U, V>> {
  static auto val(const std::pair<U, V>& p) -> const V&
  {
    return p.second;
  }
};

// use like
get<decltype(*iter)>::val(*iter);

Convenience function could look like:

template<class T>
auto getval(const T& t) -> decltype(get<T>::val(t))
{
  return get<T>::val(t);
}
2
  • I'll give this an upvote if you add the convenience function. I started to do it on the fly, but it's not trivial. <g> Aug 16, 2012 at 19:08
  • 1
    There's no reason for the auto return values for val(...) is there?
    – David
    Aug 16, 2012 at 19:20
3

You can overload the function based on the input:

void foo(const std::vector<int>::iterator& it1, const std::vector<int>::iterator& it2) 
{
   //use *it
}
void foo(const std::map<int,int>::iterator& it1, const std::map<int,int>::iterator& it2)
{
   //use it->second
}

Edit:

I think this is the closest you can get to what you want to achieve:

template <typename T, typename X>
void foo(T const& x, X const& y)
{

}

template <typename T, typename S>
void foo(const typename std::map<T,S>::iterator& x, const typename std::map<T,S>::iterator& y)
{

}

int main()
{
    std::map<int,int> x;
    std::vector<int> y;
    foo(x.begin(), x.end()); //will call second version
    foo(y.begin(), y.end()); //will call first version
}
2
  • perhaps I was too specific in the title, but I was hoping to avoid specific container names, so I wouldn't have to write a separate function for every container type. Maybe I'm looking for a way to overload based on the value_type being a pair or not. Aug 16, 2012 at 18:57
  • @JohnGordon see edited answer, perhaps it's closer to what you need. Aug 16, 2012 at 19:01
2

A trait should do the trick. First the type-deducing helper:

template <typename Iter>
typename iter_value<Iter>::value_type & iter_deref(Iter it)
{
    return iter_value<Iter>::deref(it);
}

All we need is something like this:

template <typename Iter>
class iter_value
{
    template <typename T> struct aux
    {
        typedef T type;
        static type & deref(Iter it) { return *it; }
    };
    template <typename U, typename V> struct aux<std::pair<U const, V>>
    {
        typedef V type;
        static type & deref(Iter it) { return it->second; }
    };

    typedef typename std::iterator_traits<Iter>::value_type type;

public:
    typedef typename aux<type>::type value_type;

    static value_type & deref(Iter it)
    {
        return aux<type>::deref(it);
    }
};
1

You can create a function that extracts the value out of the iterator. Then you can overload that based on the type of the iterator. You can use that function in your algorithm. Assuming a vector of ints and a map of string->int, it could look like this:

int getValue(const std::vector<int>::iterator& it)
{
    return *it;
}

int getValue(const std::map<std::string, int>::iterator& it)
{
    return it->second;
}

Then the algorithm can use the function getValue() to get a value from the iterator.

Your Answer

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

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