cpp/container/map
From cppreference.com
{"CPU", 10}, {"GPU", 15}, {"RAM", 20}};
print_map("1) Initial map: ", m);
m["CPU"] = 25; // update an existing value m["SSD"] = 30; // insert a new value print_map("2) Updated map: ", m);
// using operator[] with non-existent key always performs an insert std::cout << "3) m[UPS] = " << m["UPS"] << '\n'; print_map("4) Updated map: ", m);
m.erase("GPU"); print_map("5) After erase: ", m);
std::erase_if(m, [](const auto& pair){ return pair.second > 25; }); print_map("6) After erase: ", m); std::cout << "7) m.size() = " << m.size() << '\n';
m.clear(); std::cout << std::boolalpha << "8) Map is empty: " << m.empty() << '\n';
} |output= 1) Initial map: [CPU] = 10; [GPU] = 15; [RAM] = 20; 2) Updated map: [CPU] = 25; [GPU] = 15; [RAM] = 20; [SSD] = 30; 3) m[UPS] = 0 4) Updated map: [CPU] = 25; [GPU] = 15; [RAM] = 20; [SSD] = 30; [UPS] = 0; 5) After erase: [CPU] = 25; [RAM] = 20; [SSD] = 30; [UPS] = 0; 6) After erase: [CPU] = 25; [RAM] = 20; [UPS] = 0; 7) m.size() = 3 8) Map is empty: true }}
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 230 | C++98 | Key was not required to be CopyConstructible(a key of type Key might not be able to be constructed)
|
Key is also required tobe CopyConstructible |
LWG 464 | C++98 | accessing a const map by key was inconvenient
|
at function provided
|
See also
(C++11) |
collection of key-value pairs, hashed by keys, keys are unique (class template) |
-->