Kodewick
Start learning →
Basic←→0 / 60

Running low

#030BasicMapsvector4 min

Before closing, the bakery checks what is nearly sold out so it can bake more tomorrow. Write low_stock(stock) to return a vector of the names with fewer than 3 left, in alphabetical order (the order the map already has).

Examples
low_stock({{"bun", 5}, {"pie", 2}, {"cake", 0}})→['cake', 'pie']
low_stock({{"tart", 4}})→[]
The functionvector<string> low_stock(const map<string, int> &stock)
main.cpp
#include <map>
#include <string>
#include <vector>
using namespace std;

vector<string> low_stock(const map<string, int> &stock) {
    vector<string> names;
    for (const auto &item : stock) {
        // your code here: if fewer than 3 are left, put the name in names
    }
    return names;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.