将地图复制到矢量

发布于 2024-09-13 14:59:18 字数 788 浏览 1 评论 0原文

我必须将 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 上也没有得到任何有用的结果。 这个问题类似

I have to copy certain elements from a std::map into a vector.
It should work like in this loop:

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

Since I'd like to avoid using any functors, but use boost::lambda instead, I tried using std::copy, but can't get it right.

std::copy (map.begin(), map.end(), std::back_inserter(mVec)
                bind(&std::map<int, void*>::value_type::first, _1) % 2 == 0);

I'm new to lambda expressions, and I can't figure it out how to use them correctly.
I didn't get any useful results on Google or StackOverflow either.
This question is similar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎盛开的樱花 2024-09-20 14:59:18

在STL 中你需要的是一个transform_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 );

transform_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 算法同时执行这两个步骤(转换和条件复制)。

What you would need in STL would be a transform_if algorithm. Then you would have to write:

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

The code for transform_if is taken from this unrelated question and it is:

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

I think there is no other way to perform both steps (transform and conditional copy) at once using STL algorithms.

此岸叶落 2024-09-20 14:59:18

您可以使用 升压范围适配器来实现这一点。

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

You can use boost range adaptors to achieve that.

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文