Hidden insertions in a stock map

In a small warehouse program, stock is a std::map from std::string keys to int values, and it starts empty. The program then runs these statements in order:


C++
stock["bolt"] = 40;
stock["nut"] += 25;
stock["washer"];
if (stock["gasket"] > 0) {
    stock["gasket"] -= 1;
}
if (stock.find("rivet") != stock.end()) {
    stock["rivet"] = 10;
}
stock.at("bolt") -= 15;

int total = 0;
for (const auto& entry : stock) {
    total += entry.second;
}


After all statements have run, give each quantity as a whole number: the value of stock.size(), the value of stock.count("gasket"), the value stored for the key bolt, and the final value of total.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More C++ Standard Library Questions: