我正在实现的算法具有以下结构:
while C is not empty
select a random entry e from C
if some condition on e
append some new entries to C (I don't care where)
else
remove e from C
重要的是循环 e 的每次迭代都是随机选择的(以均匀的概率)。
理想情况下select,append和remove步骤都是 O(1)。
如果我理解正确,使用std::listandappend步骤remove将是 O(1) 但随机选择将是 O(n) (例如,std::advance在此解决方案中使用)。
并且似乎有互补的 O(1) 和 O(n) 操作std::deque。std::vector
我猜这std::set会引入一些 O(log n) 复杂性。
是否有任何 stl 容器支持我在恒定时间(或摊销恒定时间)内需要的所有三个操作?