C++ 如何从 std::map 中过滤项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/180516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to filter items from a std::map?
提问by 1800 INFORMATION
I have roughly the following code. Could this be made nicer or more efficient? Perhaps using std::remove_if
? Can you remove items from the map while traversing it? Can we avoid using the temporary map?
我大致有以下代码。这可以做得更好或更有效吗?也许使用std::remove_if
? 您可以在遍历地图时从地图中删除项目吗?我们可以避免使用临时地图吗?
typedef std::map<Action, What> Actions;
static Actions _actions;
bool expired(const Actions::value_type &action)
{
return <something>;
}
void bar(const Actions::value_type &action)
{
// do some stuff
}
void foo()
{
// loop the actions finding expired items
Actions actions;
BOOST_FOREACH(Actions::value_type &action, _actions)
{
if (expired(action))
bar(action);
else
actions[action.first]=action.second;
}
}
actions.swap(_actions);
}
回答by Martin York
A variation of Mark Ransom algorithm but without the need for a temporary.
Mark Ransom 算法的一种变体,但不需要临时。
for(Actions::iterator it = _actions.begin();it != _actions.end();)
{
if (expired(*it))
{
bar(*it);
_actions.erase(it++); // Note the post increment here.
// This increments 'it' and returns a copy of
// the original 'it' to be used by erase()
}
else
{
++it; // Use Pre-Increment here as it is more effecient
// Because no copy of it is required.
}
}
回答by Mark Ransom
You could use erase(), but I don't know how BOOST_FOREACH will handle the invalidated iterator. The documentation for map::erasestates that only the erased iterator will be invalidated, the others should be OK. Here's how I would restructure the inner loop:
您可以使用擦除(),但我不知道 BOOST_FOREACH 将如何处理无效的迭代器。map::erase的文档说明只有被擦除的迭代器会失效,其他的应该没问题。以下是我将如何重组内部循环:
Actions::iterator it = _actions.begin();
while (it != _actions.end())
{
if (expired(*it))
{
bar(*it);
Actions::iterator toerase = it;
++it;
_actions.erase(toerase);
}
else
++it;
}
回答by coppro
Something that no one ever seems to know is that erase returns a new, guaranteed-to-be-valid iterator, when used on any container.
似乎没有人知道,当在任何容器上使用时,erase 会返回一个新的、保证有效的迭代器。
Actions::iterator it = _actions.begin();
while (it != _actions.end())
{
if (expired(*it))
{
bar(*it);
it = _actions::erase(it);
}
else
++it;
}
Storing actions.end() is probably not a good plan in this case since iterator stability is not guaranteed, I believe.
在这种情况下,存储 actions.end() 可能不是一个好的计划,因为不能保证迭代器的稳定性,我相信。
回答by Greg Rogers
If the idea is to remove expired items, why not use map::erase? This way you only have to remove elements you don't need anymore, not rebuild an entire copy with all the elements that you want to keep.
如果想法是删除过期项目,为什么不使用map::erase?通过这种方式,您只需删除不再需要的元素,而无需使用要保留的所有元素重建整个副本。
The way you would do this is to save off the iterators pointing to the elements you want to erase, then erase them all after the iteration is over.
这样做的方法是保存指向要擦除的元素的迭代器,然后在迭代结束后将它们全部擦除。
Or, you can save off the element that you visited, move to the next element, and then erase the temporary. The loop bounds get messed up in your case though, so you have to fine tune the iteration yourself.
或者,您可以保存您访问过的元素,移动到下一个元素,然后删除临时元素。但是,循环边界在您的情况下会变得混乱,因此您必须自己微调迭代。
Depending on how expired() is implemented, there may be other better ways. For example if you are keeping track of a timestamp as the key to the map (as expired() implies?), you can do upper_bound on the current timestamp, and all elements in the range [ begin(), upper_bound() ) need to be processed and erased.
根据 expired() 的实现方式,可能还有其他更好的方法。例如,如果您要跟踪时间戳作为映射的键(如 expired() 暗示的那样?),您可以对当前时间戳执行 upper_bound,并且范围 [ begin(), upper_bound() ) 中的所有元素都需要进行处理和删除。