Skip to content

Instantly share code, notes, and snippets.

@daniel-j-h
Created August 10, 2016 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniel-j-h/b8f09d4c014038263110b99ab17c9f51 to your computer and use it in GitHub Desktop.
Save daniel-j-h/b8f09d4c014038263110b99ab17c9f51 to your computer and use it in GitHub Desktop.
For Karen: OutputIterator example for unordered_map
#include <unordered_map>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
int main() {
// Empty int,string map
std::unordered_map<int, std::string> m;
// OutputIterator for inserting elements into the map
auto out = std::inserter(m, std::end(m));
// Just some data (note the duplicate 1 key)
std::vector<std::pair<int, std::string>> v{{1, "a"}, {2, "b"}, {1, "c"}};
// Copy the dummy data "into" the OutputIterator (which inserts into the map)
std::copy(std::begin(v), std::end(v), out);
// Print map (note how we get the first 1,a pair since insert only inserts if the element is not already present)
for (const auto& each : m)
std::cout << each.first << " : " << each.second << "\n";
// 2 : b
// 1 : a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment