简体   繁体   English

在C ++中访问无序映射内列表的元素

[英]Access elements of a list inside an unordered map in C++

My essential doubt is simply this: Suppose I have a map like this 我的基本疑问很简单:假设我有一张这样的地图

std::map<int,std::list<int>> my_map;

And now I want to copy each int of the list inside the map into a set of ints. 现在,我想将地图内列表的每个int复制到一组int中。 This is the master piece to a solution that I am doing and it misses this part only... I just want to know how to make this process of a list inside a map to a set. 这是我正在做的解决方案的杰作,它只漏掉了这一部分...我只想知道如何将映射内的列表过程变成集合。

First off, you appear to be copying... instead of loop inserting, try using: 首先,您似乎正在复制...而不是循环插入,请尝试使用:

void insert (InputIterator first, InputIterator last);

So for you that becomes (no for loop needed): 因此,对于您来说(不需要for循环):

visitados.insert(graph.begin(), graph.end());

I think you're just asking how do you get the data out of that the new map? 我想您只是在问如何从新地图中获取数据? In an unordered_map you have the syntax correct, you may use the [] operator, but unless you need to squeeze that extra bit of performance out you should stick with std::map. 在unordered_map中,您可以使用正确的语法,可以使用[]运算符,但是除非需要减少额外的性能,否则应坚持使用std :: map。 Especially given your difficulties with the STL so far. 特别是考虑到到目前为止您在使用STL时遇到的困难。

Also, I would like to stress using functions such as sort (on a regular map), find, and iterators to make things safer and easier. 另外,我想强调使用排序(在常规地图上),查找和迭代器之类的功能,以使事情更安全,更轻松。

Read more here . 在这里阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM