聽起來像是std::remove_copy_if
和std::remove_if
工作:(沒有測試過這一點,但它應該給你的想法...)
#include <algorithm>
#include <functional>
//I'm sure you probably already have a delete functor lying around, right?
// No? Well here's one for you....
struct Deleter : public std::unary_function<AguiTimedEvent*, void>
{
void operator()(AguiTimedEvent* toNuke)
{
delete toNuke;
}
};
std::vector<AguiTimedEvent*> toRun;
std::remove_copy_if(timedEvents.begin(), timedEvents.end(),
std::back_inserter(toRun), std::not1(std::mem_fun(&AguiTimedEvent::expired)));
timedEvents.erase(std::remove_if(timedEvents.begin(), timedEvents.end(),
std::mem_fun(&AguiTimedEvent::expired), timedEvents.end());
std::for_each(toRun.begin(), toRun.end(),
std::mem_fun(&AguiTimedEvent::timedEventCallback));
std::for_each(toRun.begin(), toRun.end(), Deleter());
注意,這種解決方案需要線性時間,而你需要四次時間。這也乾淨地擺脫了回調可能添加到新矢量的問題,通過刪除關於該矢量的決定直到已經完成從矢量的移除爲止。另一方面,它檢查expired
標誌兩次,所以如果這是一個複雜的操作,這可能會更慢。
什麼是確切的錯誤文本? – Tom 2010-12-21 23:20:51
表達式:向量迭代器不可忽略 (調試斷言失敗) – jmasterx 2010-12-21 23:22:42
`timedEventCallback`或`AguiTimedEvent`的析構函數是否直接或間接修改`timedEvents`? – 2010-12-21 23:28:36