A functor that reverses a pair: first becomes second,
second becomes first. Useful for switching
from a map to another map (or multimap in this example) where
the value becomes the key and the key the value.
1 2 3 4 5 6 7 |
std::map<std::string, int> m; std::multimap<int, std::string> m2; m["a"] = 0; m["b"] = 1; m["c"] = 0; std::transform (m.begin(), m.end(), std::inserter (m2, m2.begin()), pair_switch<std::map<std::string, int>::value_type> ()); |
And the actual code:
1 2 3 4 5 6 7 8 9 10 |
template<typename PairType> struct pair_switcher : public std::unary_function<pairType, std::pair<typename PairType::second_type, typename PairType::first_type> > { typedef std::pair<typename PairType::second_type, typename PairType::first_type> result_type; typedef PairType argument_type; result_type operator()(const argument_type &p) const { return result_type (p.second, p.first); } }; |
Leave a Reply