<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 中过滤项目?

        How to filter items from a std::map?(如何从 std::map 中过滤项目?)

          <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 中过滤项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(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>
                          <tbody id='PXvb2'></tbody>
                        <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>