Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save make-github-pseudonymous-again/2f88f77d89f8707bea12 to your computer and use it in GitHub Desktop.
Save make-github-pseudonymous-again/2f88f77d89f8707bea12 to your computer and use it in GitHub Desktop.
en.cppreference.com example for emplace construction in unordered maps
#include <iostream>
#include <utility>
#include <unordered_map>
int main(){
std::unordered_map<std::string, std::string> m;
// uses pair's copy-constructor
m.emplace(std::make_pair(std::string("a"), std::string("a")));
// uses pair's converting copy constructor
m.emplace(std::make_pair("b", "abcd"));
// uses pair's template constructor
m.emplace("d", "ddd");
// uses pair's piecewise constructor
m.emplace(std::piecewise_construct,
std::forward_as_tuple("c"),
std::forward_as_tuple(10, 'c'));
for (const auto &p : m) {
std::cout << p.first << " => " << p.second << '\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment