1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
// key_type: string mapped_type: set of int
std::map< std::string, std::set<int> > fishMap { { "carp", { 5, 2, 7 } }, { "pike", { 22, 7, 6, 9 } } } ;
// key "pike" exists, get a reference to the mapped value (set of int)
auto& fishIdSet = fishMap[ "pike" ];
// key "salmon" does not exist, inset the pair { "salmon", empty set of int } into the map
// get a reference to the mapped value (empty set of int)
auto& fishIdSet2 = fishMap[ "salmon" ];
// key "carp" exists, insertion fails. return pair { iterator, false }
// pair.second == false (not inserted)
const auto pair = fishMap.insert( { "carp", { 1, 2, 3, 4 } } ) ;
// key "minnow" does not exist, insert pair { "minnow", { 1, 2, 3, 4 } } into the map
// return pair { iterator, true } pair2.second == true (inserted)
const auto pair2 = fishMap.insert( { "minnow", { 1, 2, 3, 4 } } ) ;
// key "pike" exists, get a reference to the mapped value (set of int), assign set { 1, 2, 3, 4 } to it
// at the end, key == "pike", mapped value == set { 1, 2, 3, 4 }
fishMap[ "pike" ] = { 1, 2, 3, 4 } ;
// key "mackerel" does not exist, inset the pair { "salmon", empty set of int } into the map
// get a reference to the mapped value (empty set), assign set { 5, 6, 7 } to it
// at the end, key == "mackerel", mapped value == set { 5, 6, 7 }
fishMap[ "mackerel" ] = { 5, 6, 7 } ;
|