相关疑难解决方法(0)

如何过滤std :: 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);
}
Run Code Online (Sandbox Code Playgroud)

c++ boost stl

27
推荐指数
3
解决办法
2万
查看次数

标签 统计

boost ×1

c++ ×1

stl ×1