Skip to content

Instantly share code, notes, and snippets.

@SethHamilton
Last active November 21, 2017 15:53
Show Gist options
  • Save SethHamilton/77a236aabbbcb79f848a8c40e2adf07a to your computer and use it in GitHub Desktop.
Save SethHamilton/77a236aabbbcb79f848a8c40e2adf07a to your computer and use it in GitHub Desktop.
std::unordered_map - Header only reverse map from forward map using lambda
#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