【发布时间】:2012-02-28 06:08:30
【问题描述】:
我正在迭代一组回调函数。函数在迭代过程中被调用,可能会导致函数集的实际容器发生巨大变化。
我现在做的是:
- 复制原始集
- 遍历副本,但对每个元素检查它是否仍然存在于原始集合中
检查每个元素的存在是超级动态的,但似乎也很慢。
是否有其他建议可以解决此案?
编辑:这是实际代码:
// => i = event id
template <class Param>
void dispatchEvent(int i, Param param) {
EventReceiverSet processingNow;
const EventReceiverSet& eventReceiverSet = eventReceiverSets[i];
std::copy(eventReceiverSet.begin(), eventReceiverSet.end(), std::inserter(processingNow, processingNow.begin()));
while (!processingNow.empty()) {
EventReceiverSet::iterator it = processingNow.begin();
IFunction<>* function = it->getIFunction(); /// get function before removing iterator
processingNow.erase(it);
// is EventReceiver still valid? (may have been removed from original set)
if (eventReceiverSet.find(ERWrapper(function)) == eventReceiverSet.end()) continue; // not found
function->call(param);
}
};
【问题讨论】:
-
“设置”是指
std::set<>?如果不是,实际的容器类型是什么? -
好吧,我想保留这个通用的,但是是的,它是一个 std::set