Reversing or reverse iterating an unordered_map

与世无争的帅哥 提交于 2020-01-02 02:12:50

问题


I'm trying to print the contents of an unordered_map in reverse order, but it doesn't have rbegin or rend, so you can't use reverse_iterator. How is reversing of the unordered_map supposed to be done?

EDIT (from comment): I want the keys to be in the order they were inserted in, hence I cant use map. The keys appear to stay in the insertion order, but I need them reversed.


回答1:


Just reading the first sentence in your question gives you the answer:

I'm trying to print the contents of an unordered_map in reverse order

You cannot print in any order because, well, it is unordered. It makes no sense talking about order in an unordered structure. It does not matter how the contents of an unordered_map are organised internally: this is an implementation detail and you cannot access it. To the outside world, an unordered_map exhibits no order at all and you cannot expect it to do it.




回答2:


You can cobble up your own pair of reverse iterators from any pair of bidirectional iterators:

std::reverse_iterator rbegin(first);
std::reverse_iterator rend(last);

As @Tin suggested, this doesn't work. Never mind.




回答3:


Insert the content of your unordered map into another unoredered map. Do remember inserting using make_pair in a pair form so as to retain your data.




回答4:


When you use an unordered_map no particular order is going to be maintained because the key values will have an implementation based hash code.

As you mention that you need them in the order you insert -- most probably you will be iterating through some kind of a linear list and storing key values pair in the unordered map. Instead use an ordered_map and insert based on indices of the list. In this case, your order will be maintained as your inserting into the map based on index as key.




回答5:


Firstly, this type of thing can be used when you are given some input in form of key-value pairs and you need to do some operation on that and print some result in order of the input received.

So you can use an unordered map.

As said in above posts, unordered map is completely unordered, so a good way of doing this is to have a vector to have your keys and then having all key-value pairs in an unordered map. Then using key values in vector first to find the values in map using find() and then an iterator printing key-value pairs.

See the below example:

Suppose you want to take two integers as an input in form of key-value pair and for purpose of doing some operations on it, you use an unordered map, and now you want to print values in the order of input.

vector<int> v;
unordered_map<int,int> m;
unordered_map<int,int>::iterator it;

Now when you want to print output in order of input.

for (int i=0;i<v.size();i++){
        it=m.find(v[i]);
        if(it!=m.end())
            cout << it->first << " " << it-second << "\n";
}

This will print your key-value pairs in the order of the input.

I hope this helps!




回答6:


had the same issue, unordered_map stores data in reverse order

so

map[A]=X
map[B]=Y
map[C]=Z

if you iterate for(map:iterator it=begin...) you'll get

Z
Y
X

so it implies an order, witch is reversed



来源:https://stackoverflow.com/questions/13643815/reversing-or-reverse-iterating-an-unordered-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!