迭代器适配器只迭代映射中的值? [英] iterator adapter to iterate just the values in a map?

查看:144
本文介绍了迭代器适配器只迭代映射中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是回到C ++经过几年的做了很多C#,最近的目标C。

I'm just getting back into C++ after a couple of years of doing a lot of C#, and recently Objective C.

我做了一件事是滚动我自己的std :: map的迭代器适配器,它将deref只是值部分,而不是键值对。这是一个相当普遍和自然的事情。 C#为此工具提供了其Dictionary类的Keys和Values属性。 Objective-C的NSDictionary类似地有allKeys和allValues。

One thing I've done before is to roll my own iterator adapter for std::map that will deref to just the value part, rather than the key-value pair. This is quite a common and natural thing to do. C# provides this facility with its Keys and Values properties of its Dictionary class. Objective-C's NSDictionary, similarly, has allKeys and allValues.

由于我已经离开,Boost已经获得了Range和ForEach库,广泛。我想知道是否两者之间有一些设施,做同样的,但我还没有能够找到任何东西。

Since I've been "away", Boost has acquired the Range and ForEach libraries, which I am now using extensively. I wondered if between the two there was some facility to do the same, but I haven't been able to find anything.

我想用敲门Boost的迭代器适配器,但在我走下这条路线之前,我想我会问这里是否有人知道这样的设施在Boost,或其他准备做的其他地方。

I'm thinking of knocking something up using Boost's iterator adapters, but before I go down that route I thought I'd ask here if anyone knows of such a facility in Boost, or somewhere else ready made?

推荐答案

我不认为有什么开箱即用。可以使用boost :: make_transform。

I don't think there's anything out of the box. You can use boost::make_transform.

template<typename T1, typename T2> T2& take_second(const std::pair<T1, T2> &a_pair) 
{
  return a_pair.second;
}

void run_map_value()
{
  map<int,string> a_map;
  a_map[0] = "zero";
  a_map[1] = "one";
  a_map[2] = "two";
  copy( boost::make_transform_iterator(a_map.begin(), take_second<int, string>),
    boost::make_transform_iterator(a_map.end(), take_second<int, string>),
    ostream_iterator<string>(cout, "\n")
    );
}

这篇关于迭代器适配器只迭代映射中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆