Created
November 12, 2019 14:27
-
-
Save AntonioCS/c52a6d46a0bd7e38c047366289fdd92b 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 <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