翻转地图键值对 [英] Flip map key-value pair
本文介绍了翻转地图键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例地图:
1 - 1.0
2 - 2.0
翻转后
1.0 - 1
2.0 - 2
解决方案
最简单的方法是(我知道的)创建一个新的类型被翻转的地图,并迭代旧的,并相反地添加每个键值对。 p>
例如,
map< int,float> if_map;
//将一些项目插入if_map
if_map [1] = 43.11;
if_map [44] = -13421.438;
map< float,int>逆转;对于(map< int,float> :: iterator i = if_map.begin(); i!= if_map.end(); ++ i)
reverse [i->第二] = i->第一;
I have a map. I want to flip the key-value so that it not becomes map. So basically the value of the first map becomes the key of the second map. How do i do this?
Example map:
1 - 1.0
2 - 2.0
After flip
1.0 - 1
2.0 - 2
解决方案
The most straightforward way (that I know of) is to create a new map with the types flipped, and iterate the old one and add each key-value pair in reverse.
For example,
map<int, float> if_map;
// insert some items into if_map
if_map[1] = 43.11;
if_map[44] = -13421.438;
map<float, int> reversed;
for (map<int, float>::iterator i = if_map.begin(); i != if_map.end(); ++i)
reversed[i->second] = i->first;
这篇关于翻转地图键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文