Last active
August 4, 2016 10:14
-
-
Save Drunkar/7763993 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <map> | |
#include <string> | |
#include <algorithm> | |
#include <iostream> | |
template<typename A, typename B> | |
std::pair<B,A> flip_pair(const std::pair<A,B> &p) | |
{ | |
return std::pair<B,A>(p.second, p.first); | |
} | |
template<typename A, typename B> | |
std::map<B,A> flip_map(const std::map<A,B> &src) | |
{ | |
std::map<B,A> dst; | |
std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()), | |
flip_pair<A,B>); | |
return dst; | |
} | |
int main(void) | |
{ | |
std::map<std::string, int> src; | |
src["a"] = 3; | |
src["b"] = 1; | |
src["c"] = 2; | |
std::map<int, std::string> dst = flip_map(src); | |
for( std::pair<int, std::string> item : dst) | |
std::cout << item.first << "," << item.second << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from: http://stackoverflow.com/questions/5056645/sorting-stdmap-using-value