简体   繁体   中英

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. 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):

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. Especially given your difficulties with the STL so far.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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