我發現自己寫的代碼看起來像這樣很多:檢查列表和運行處理
set<int> affected_items;
while (string code = GetKeyCodeFromSomewhere())
{
if (code == "some constant" || code == "some other constant") {
affected_items.insert(some_constant_id);
} else if (code == "yet another constant" || code == "the constant I didn't mention yet") {
affected_items.insert(some_other_constant_id);
} // else if etc...
}
for (set<int>::iterator it = affected_items.begin(); it != affected_items.end(); it++)
{
switch(*it)
{
case some_constant_id:
RunSomeFunction(with, these, params);
break;
case some_other_constant_id:
RunSomeOtherFunction(with, these, other, params);
break;
// etc...
}
}
我結束了寫這個代碼的原因是,我只需要在第二循環中運行的功能甚至一度如果我收到了可能導致它們運行的多個鍵碼。
這似乎不是最好的方式來做到這一點。有沒有更好的方法?
我喜歡todo-list的想法。 – 2008-10-28 03:29:34