简体   繁体   English

如何从boost :: multi_index中删除多个元素?

[英]How to remove multiple elements from a boost::multi_index?

I am trying to do something like this: 我正在尝试做这样的事情:

auto& myIndex = myMultiIndex.get<0>();
auto range = myIndex.equal_range(x);
for (auto iter = range.first; iter != range.second; ++iter) {
    if (somePredicate) myIndex.erase(iter);
}

Of course this does not work because the iterator becomes invalid once we erase an element from the container. 当然这是行不通的,因为一旦我们从容器中删除了一个元素,迭代器就变得无效了。 Also std::remove_if doesn't work since it modifies and overwrites elements in the container which will mess up the other indices. 另外std :: remove_if也不起作用,因为它修改并覆盖了容器中的元素,这会弄乱其他索引。 What is the recommended way of doing something like this? 建议做这种事情的方法是什么?

Thanks! 谢谢!

for (auto iter = range.first; iter != range.second;) {
    if (somePredicate) iter = myIndex.erase(iter);
    else ++iter;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM