我正在遍历一组回调函数。在迭代过程中调用这些函数可能会导致对函数集合的实际容器进行重大更改。
我现在做的是:
1. 复制原始集合 2. 遍历副本,但对于每个元素都检查它是否仍然存在于原始集合中
检查每个元素的存在性非常动态,但似乎也相当慢。
有其他提议来解决这种情况吗?
编辑:这是实际代码:
我现在做的是:
1. 复制原始集合 2. 遍历副本,但对于每个元素都检查它是否仍然存在于原始集合中
检查每个元素的存在性非常动态,但似乎也相当慢。
有其他提议来解决这种情况吗?
编辑:这是实际代码:
// => 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<>
吗?如果不是,那么实际的容器类型是什么? - ildjarn