#include <iostream>
#include <map>
#include <string>
// *** before: ***
void oldWay(std::map<std::string, int>& ages) {
std::map<std::string, int>::iterator it = ages.find("Ann");
if (it != ages.end())
std::cout << it->second << "\n";
// it goes on living to the end of the function, and the next block
// has to invent another name
int size = static_cast<int>(ages.size());
switch (size) {
case 0: std::cout << "empty\n"; break;
default: std::cout << size << "\n";
}
}
// *** in version C++17: ***
void newWay(std::map<std::string, int>& ages) {
if (auto it = ages.find("Ann"); it != ages.end())
std::cout << it->second << "\n";
else
std::cout << "not found\n"; // it is visible in the else as well
// and nowhere after the if
switch (int size = static_cast<int>(ages.size()); size) {
case 0: std::cout << "empty\n"; break;
default: std::cout << size << "\n";
}
}
int main() {
std::map<std::string, int> ages{{"Ann", 31}, {"Bob", 27}};
oldWay(ages);
newWay(ages);
}