<i id='ma5PQ'><tr id='ma5PQ'><dt id='ma5PQ'><q id='ma5PQ'><span id='ma5PQ'><b id='ma5PQ'><form id='ma5PQ'><ins id='ma5PQ'></ins><ul id='ma5PQ'></ul><sub id='ma5PQ'></sub></form><legend id='ma5PQ'></legend><bdo id='ma5PQ'><pre id='ma5PQ'><center id='ma5PQ'></center></pre></bdo></b><th id='ma5PQ'></th></span></q></dt></tr></i><div id='ma5PQ'><tfoot id='ma5PQ'></tfoot><dl id='ma5PQ'><fieldset id='ma5PQ'></fieldset></dl></div>

    • <bdo id='ma5PQ'></bdo><ul id='ma5PQ'></ul>

        <small id='ma5PQ'></small><noframes id='ma5PQ'>

      1. <legend id='ma5PQ'><style id='ma5PQ'><dir id='ma5PQ'><q id='ma5PQ'></q></dir></style></legend>

        <tfoot id='ma5PQ'></tfoot>

      2. 如何从 std::map 中过滤项目?

        时间:2023-06-29

          <small id='jVUkQ'></small><noframes id='jVUkQ'>

          <tfoot id='jVUkQ'></tfoot>
            <tbody id='jVUkQ'></tbody>

              <bdo id='jVUkQ'></bdo><ul id='jVUkQ'></ul>
              <i id='jVUkQ'><tr id='jVUkQ'><dt id='jVUkQ'><q id='jVUkQ'><span id='jVUkQ'><b id='jVUkQ'><form id='jVUkQ'><ins id='jVUkQ'></ins><ul id='jVUkQ'></ul><sub id='jVUkQ'></sub></form><legend id='jVUkQ'></legend><bdo id='jVUkQ'><pre id='jVUkQ'><center id='jVUkQ'></center></pre></bdo></b><th id='jVUkQ'></th></span></q></dt></tr></i><div id='jVUkQ'><tfoot id='jVUkQ'></tfoot><dl id='jVUkQ'><fieldset id='jVUkQ'></fieldset></dl></div>
              <legend id='jVUkQ'><style id='jVUkQ'><dir id='jVUkQ'><q id='jVUkQ'></q></dir></style></legend>

                  本文介绍了如何从 std::map 中过滤项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我大致有以下代码.这可以做得更好或更有效吗?也许使用 std::remove_if ?您可以在遍历地图时从地图中删除项目吗?我们可以避免使用临时地图吗?

                  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?

                  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);
                  }
                  

                  推荐答案

                  你可以使用 erase(),但我不知道 BOOST_FOREACH 将如何处理失效的迭代器.map::erase 的文档 指出只有被擦除的迭代器才会被删除失效了,其他的应该没问题.下面是我将如何重构内部循环:

                  You could use erase(), but I don't know how BOOST_FOREACH will handle the invalidated iterator. The documentation for map::erase states that only the erased iterator will be invalidated, the others should be OK. Here's how I would restructure the inner loop:

                  Actions::iterator it = _actions.begin();
                  while (it != _actions.end())
                  {
                    if (expired(*it))
                    {
                      bar(*it);
                      Actions::iterator toerase = it;
                      ++it;
                      _actions.erase(toerase);
                    }
                    else
                      ++it;
                  }
                  

                  这篇关于如何从 std::map 中过滤项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:链接 boost 日志教程时的链接器错误(未定义的参考) 下一篇:转义 C++ 字符串

                  相关文章

                  <small id='PXvb2'></small><noframes id='PXvb2'>

                  • <bdo id='PXvb2'></bdo><ul id='PXvb2'></ul>

                • <legend id='PXvb2'><style id='PXvb2'><dir id='PXvb2'><q id='PXvb2'></q></dir></style></legend>
                  <i id='PXvb2'><tr id='PXvb2'><dt id='PXvb2'><q id='PXvb2'><span id='PXvb2'><b id='PXvb2'><form id='PXvb2'><ins id='PXvb2'></ins><ul id='PXvb2'></ul><sub id='PXvb2'></sub></form><legend id='PXvb2'></legend><bdo id='PXvb2'><pre id='PXvb2'><center id='PXvb2'></center></pre></bdo></b><th id='PXvb2'></th></span></q></dt></tr></i><div id='PXvb2'><tfoot id='PXvb2'></tfoot><dl id='PXvb2'><fieldset id='PXvb2'></fieldset></dl></div>
                      <tfoot id='PXvb2'></tfoot>