Last active
August 29, 2015 13:56
-
-
Save DieHertz/8971454 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 <unordered_map> | |
#include <utility> | |
#include <iostream> | |
using namespace std; | |
template<class T1, class T2> void fill(unordered_map<T1, T2>& m) {} | |
template<class T1, class T2, class Key, class Value, class... Args> | |
void fill(unordered_map<T1, T2>& m, Key&& key, Value&& value, Args&&... args) { | |
m.insert({ forward<Key>(key), forward<Value>(value) }); | |
fill(m, forward<Args>(args)...); | |
} | |
template<class T1, class T2, class... Args> | |
unordered_map<T1, T2> unordered_map_init(Args&&... args) { | |
auto m = unordered_map<T1, T2>{}; | |
fill(m, forward<Args>(args)...); | |
return m; | |
} | |
int main() { | |
static auto m = unordered_map_init<int, int>(1, 2, 3, 4); | |
for (auto& p : m) cout << p.first << " => " << p.second << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment