I have the following template function that returns a copy of this map with the changed keys and values:
template<typename M> auto swapKeysAndValues(const M& m) { std::map<typename M::mapped_type, typename M::key_type> swapped; for (auto& p : m) { swapped.emplace(p.second, p.first); } return swapped; }
Is there a way to make the template above for std::map and std::unordered_map ? That is, for std::map<K, V> it should return std::map<V, K> , and for std::unordered_map<K, V> it should return std::unordered_map<V, K> .
source share