我有以下代碼的順序:一個C++哈希表,保留插入
#include <iostream>
#include "boost/unordered_map.hpp"
using namespace std;
using namespace boost;
int main()
{
typedef unordered_map<int, int> Map;
typedef Map::const_iterator It;
Map m;
m[11] = 0;
m[0] = 1;
m[21] = 2;
for (It it (m.begin()); it!=m.end(); ++it)
cout << it->first << " " << it->second << endl;
return 0;
}
不過,我尋找的東西,保留順序,這樣以後我可以通過在同一順序的元素迭代插入它們。在我的電腦上面的代碼不保留順序,並打印如下:
0 1
11 0
21 2
我想也許我可以用一個boost::multi_index_container
typedef multi_index_container<
int,
indexed_by<
hashed_unique<identity<int> >,
sequenced<>
>
> Map;
有人能告訴我如何使用來實現我的原代碼這個容器(或任何其他適當的容器),以便迭代器遵循插入順序?
正在維護一個單獨的列表來跟蹤插入順序嗎? – Qberticus