麵包店打烊前看看哪些快賣完了,好明天多做。寫 low_stock(stock):把剩下不到 3 個的點心名字放進 vector 返回,按字母順序(就是 map 裡的順序)。
low_stock({{"bun", 5}, {"pie", 2}, {"cake", 0}})→['cake', 'pie']low_stock({{"tart", 4}})→[]vector<string> low_stock(const map<string, int> &stock)#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) {
// 在這裡寫:剩下不到 3 個,就把名字放進 names
}
return names;
}
寫好了先點「執行」試試示例,沒問題再點「提交」。