#include // for EXIT_SUCCESS #include // for std::cout, std::endl #include // for std::unique_ptr #include // for std::map using std::unique_ptr; using std::map; // Function object to use in map construction struct MapComp { bool operator()(const unique_ptr &lhs, const unique_ptr &rhs) const { return *lhs < *rhs; } }; int main(int argc, char **argv) { // Create the map map,int,MapComp> a_map; // Create the three unique_ptrs that will be keys unique_ptr a(new int(5)); unique_ptr b(new int(9)); unique_ptr c(new int(7)); // Transfer ownership of the unique_ptrs into the // map using std::move; after this, a, b, and c // will contain NULL. a_map[std::move(a)] = 25; a_map[std::move(b)] = 81; a_map[std::move(c)] = 49; // Iterate through the map, printing out the values. // Could alternatively be done with for (auto& p : a_map) { } map,int>::iterator it; for (it = a_map.begin(); it != a_map.end(); it++) { std::cout << "key: " << *(it->first); std::cout << " value: " << it->second; std::cout << std::endl; } return EXIT_SUCCESS; }