Last active
November 21, 2017 15:53
-
-
Save SethHamilton/77a236aabbbcb79f848a8c40e2adf07a to your computer and use it in GitHub Desktop.
std::unordered_map - Header only reverse map from forward map using lambda
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
#pragma once | |
#include <unordered_map> | |
#include <utility> | |
enum class test_e : int64_t | |
{ | |
one, | |
two, | |
three | |
}; | |
// forward map | |
static const std::unordered_map<string, test_e> testForwardMap = { | |
{"one", test_e::one}, | |
{"two", test_e::two}, | |
{"three", test_e::three} | |
}; | |
// reverse map | |
static const std::unordered_map<test_e, string> testReverseMap([]()->std::unordered_map<test_e,string>{ | |
std::unordered_map<test_e, string> res; | |
for (auto &i : testForwardMap) | |
res.emplace(i.second, i.first); | |
return std::move(res); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment