Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Map 将地图复制到矢量_Map_Boost Lambda - Fatal编程技术网

Map 将地图复制到矢量

Map 将地图复制到矢量,map,boost-lambda,Map,Boost Lambda,我必须将std::map中的某些元素复制到向量中。 它的工作原理应如下循环所示: typedef int First; typedef void* Second; std::map<First, Second> map; // fill map std::vector<Second> mVec; for (std::map<First, Second>::const_iterator it = map.begin(); it != map.end(); ++i

我必须将std::map中的某些元素复制到向量中。 它的工作原理应如下循环所示:

typedef int First;
typedef void* Second;
std::map<First, Second> map;
// fill map
std::vector<Second> mVec;
for (std::map<First, Second>::const_iterator it = map.begin(); it != map.end(); ++it) {
    if (it->first % 2 == 0) {
        mVec.push_back (it->second);
    }
}
由于我不想使用任何函子,而是使用boost::lambda,所以我尝试使用std::copy,但无法正确使用它

std::copy (map.begin(), map.end(), std::back_inserter(mVec)
                bind(&std::map<int, void*>::value_type::first, _1) % 2 == 0);
我不熟悉lambda表达式,我不知道如何正确使用它们。 我在Google或StackOverflow上也没有得到任何有用的结果。

在STL中,您需要的是一个变换if算法。然后你必须写:

transform_if (mymap.begin(), mymap.end(), 
     back_inserter(myvec),  
     bind(&std::map<First, Second>::value_type::second, _1) ,
     (bind(&std::map<First, Second>::value_type::first, _1) % 2) == 0 );
转换_if的代码取自,它是:

template<class InputIterator, class OutputIterator, class UnaryFunction, class Predicate>
OutputIterator transform_if(InputIterator first, 
                            InputIterator last, 
                            OutputIterator result, 
                            UnaryFunction f, 
                            Predicate pred)
{
  for (; first != last; ++first)
  {
    if( pred(*first) )
      *result++ = f(*first);
  }
  return result; 
}
我认为没有其他方法可以使用STL算法同时执行步骤转换和条件复制。

您可以使用来实现这一点

using namespace boost::adaptors;

boost::copy( map | filtered( [] (const pair<First,Second> &p)->bool {return p.first % 2 == 0;})
                 | transformed( [] (const pair<First,Second> &p) {return p.second;}),
             std::back_inserter(mVec));