具有添加顺序的C ++字典/地图 [英] C++ dictionary/map with added order
问题描述
我想拥有与map类似的东西,但是在迭代时我希望它们与添加时的顺序相同.
I want to have something similar to map but while iterating I want them to be in the same order as it is added.
示例
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);
在迭代时,我希望这些项目像一个","两个,"三个.默认情况下,map不提供此添加顺序.如何以添加的方式获取map元素?我想保留插入顺序.
While iterating I want the items to be like "one", ""two", "three"..By default, map doesn't provide this added order. How to get the map elements the way I have added? I want to retain the insertion order.
任何使用STL的工具都可以,也可以使用其他替代建议.
Anything with STL is fine or other alternative suggestions also fine.
推荐答案
A std::map that keep track of the order of insertion? this is a duplicate (thanks to neil butterworth)
您可以使用具有与之平行的排序结构的地图.
You could use a map with a sorted structure parallel to it.
map<key, value>
vector<value*> //holds only pointers to map entries.
vector<key> //holds only map keys. Adds one indirection.
这篇关于具有添加顺序的C ++字典/地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!