I tried to write a short function to invert an std::map<K, V>
(I know about boost.bimap, this is for self-education), and found, to my surprise, that the code that GCC 4.4 accepted with -pedantic -ansi settings was rejected as const-incorrect by SunCC (5.8, from 2005).
Since value_type
is std::pair<const K, V>
, SunCC insisted that I const-qualify my K
type in the arguments to functions that are passed to transform()
and for_each()
, and in the type of the return value to be passed to std::inserter
, as far as I can tell, it might be right? Which compiler was standards-compliant?
#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <algorithm>
template<typename K, typename V>
std::pair<V, K> flip_pair(const std::pair<K, V>& p) // GCC/MSVC
//std::pair<const V, K> flip_pair(const std::pair<const K, V>& p) // SunCC
{
return std::make_pair(p.second, p.first); // GCC/MSVC
// return std::pair<const V, K>(p.second, p.first); // SunCC
}
template<typename K, typename V>
std::multimap<V, K> invert_map(const std::map<K, V>& in)
{
std::multimap<V, K> out;
transform(in.begin(), in.end(), std::inserter(out, out.begin()),
flip_pair<K, V>);
return out;
}
void print_pair(const std::pair<int, std::string>& p) // GCC/MSVC
//void print_pair(const std::pair<const int, std::string>& p) // SunCC
{
std::cout << p.first << '\t' << p.second << '\n';
}
int main()
{
std::map<std::string, int> map;
map["foo"] = 1; map["bar"] = 2; map["baz"] = 3;
std::multimap<int, std::string> revmap = invert_map(map);
for_each(revmap.begin(), revmap.end(), print_pair);
}