我大致有以下代碼。這可以做得更好或更高效?也許使用std::remove_if
?你可以在遍歷它的時候從地圖中移除物品嗎?我們可以避免使用臨時地圖嗎?如何從std :: map過濾項目?
typedef std::map<Action, What> Actions;
static Actions _actions;
bool expired(const Actions::value_type &action)
{
return <something>;
}
void bar(const Actions::value_type &action)
{
// do some stuff
}
void foo()
{
// loop the actions finding expired items
Actions actions;
BOOST_FOREACH(Actions::value_type &action, _actions)
{
if (expired(action))
bar(action);
else
actions[action.first]=action.second;
}
}
actions.swap(_actions);
}
謝謝,這大概是我想出了太 – 2008-10-07 22:06:38