Created
August 10, 2016 09:52
-
-
Save daniel-j-h/b8f09d4c014038263110b99ab17c9f51 to your computer and use it in GitHub Desktop.
For Karen: OutputIterator example for unordered_map
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 <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