Skip to content

Instantly share code, notes, and snippets.

@AntonioCS
Created November 12, 2019 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntonioCS/c52a6d46a0bd7e38c047366289fdd92b to your computer and use it in GitHub Desktop.
Save AntonioCS/c52a6d46a0bd7e38c047366289fdd92b to your computer and use it in GitHub Desktop.
#include <iostream>
#include <unordered_map>
#include <string>
template <typename T1, typename T2>
std::unordered_map<T2, T1> flip(std::unordered_map<T1, T2>& m) {
std::unordered_map<T2, T1> result;
for (auto&& [key, value] : m) {
result.emplace(value, key);
}
return result;
};
int main() {
std::unordered_map<std::string, int> m{{"111", 5}};
std::cout << m.at("111") << "\n"; // 5
auto res = flip<std::string, int>(m);
std::cout << res.at(5) << "\n"; // 111
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment