做名牌前,先找出最長的名字,看名牌要多寬。寫 longest_name(names),返回最長的名字;一樣長的留前面那個;沒有名字就返回空字串 ""。
longest_name({"Amy", "Benjamin", "Zoe"})→'Benjamin'longest_name({})→''string longest_name(const vector<string> &names)#include <string>
#include <vector>
using namespace std;
string longest_name(const vector<string> &names) {
string best = "";
for (const string &name : names) {
// 在這裡寫:name 比 best 長,就換成 name
}
return best;
}
寫好了先點「執行」試試示例,沒問題再點「提交」。