从地图获取值列表
有没有一种stl方法可以从地图中获取值列表?
即,我有:
std::map<A,B> myMap;
并且我想要一个仅返回值列表的函数,即 std::list
(或为此设置。 有内置的 stl 方法可以做到这一点吗?
Is there an stl way to get a list of values from a map?
i.e, I have:
std::map<A,B> myMap;
and I would like a function that will return just the list of values, i.e, std::list<B>
(or set for that matter.
Is there a built-in stl way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
map
元素被定义为map::value_type
,其类型为pair
。first
是键,second
是值。您可以编写一个函子来从value_type中提取
,并将其复制到第二个
向量
(或列表
,或任何你想要的。)进行复制的最佳方法是使用变换
,正如其名称所暗示的那样:它接受一种类型的值并将其转换为另一种类型的值。这是一个完整的工作示例:
编辑:
如果您有支持 C++0x 的编译器lambdas,你可以完全消除函子。这对于使代码更具可读性、可论证性、更易于维护非常有用,因为您最终不会在代码库中出现数十个小的一次性函子。以下是如何更改上面的代码以使用 lambda:
A
map
element is defined as amap::value_type
, and the type of it is apair<A,B>
.first
is the key andsecond
is the value. You can write a functor to extractsecond
from avalue_type
, and copy that in to avector
(or alist
, or whatever you want.) The best way to do the copying is to usetransform
, which does just what its name implies: it takes a value of one type and transforms it to a different type of value.Here's a complete working example:
EDIT:
If you have a compiler that supports C++0x lambdas, you can eliminate the functor entirely. This is very useful for making code more readable and, arguable, easier to maintain since you don't end up with dozens of little one-off functors floating around in your codebase. Here's how you would change the code above to use a lambda:
没有内置任何东西,没有。不过,编写自己的函数非常简单:迭代地图。迭代器将为您提供一个
pair
。将每个第二
值添加到结果列表中。There's nothing built in, no. It's simple enough to write your own function, though: Iterate over the map. The iterator will give you a
pair<A, B>
. Add eachsecond
value to the result list.你不能只是“获取”这样一个列表,因为内部任何地方都没有存储任何预先存在的列表,但你可以构建一个:
或者如果你真的喜欢更 STL 的方式:
You can't just "get" such a list because there is no pre-existing list stored anywhere in the guts, but you can build one:
Or if you really like the more STL way:
许多“内置”方法之一当然是最明显的一种。只需迭代所有按键 (
pair::first
) 排序的pair 元素,并将值 (pair::second
) 添加到新容器中,您可以在可以使用正确的容量进行构造,以在迭代和添加过程中消除多余的分配。请注意:
std::list
很少是您真正想要使用的容器。当然,除非您真的非常确实需要它的特定功能。One of many "built-in" ways is of course the most obvious one. Just iterate over all pair elements, which are ordered by key (
pair::first
), and add the value (pair::second
) to a new container, which you can construct with the correct capacity to get rid of excess allocations during the iteration and adding.Just a note:
std::list
is seldom the container you actually want to be using. Unless, of course, you really, really do need its specific features.当然。
如果您没有 C++0x 编译器,首先我对您表示同情,其次您需要为此目的构建一个快速函数对象。
Sure.
If you don't have a C++0x compiler, first you have my sympathies, and second, you will need to build a quick function object for this purpose.
您可以使用boost的
transform_iterator
:http://www.boost.org/doc/libs/1_64_0/libs/iterator/doc/transform_iterator.htmlYou can use boost's
transform_iterator
: http://www.boost.org/doc/libs/1_64_0/libs/iterator/doc/transform_iterator.html