迭代 unordered_map C++
Iterating over unordered_map C++
在 unordered_map 中以特定顺序插入的键是否会在使用迭代器遍历地图时以 相同的顺序 出现?
例如:如果我们在 B 中插入 (4,3), (2, 5), (6, 7)。
并像这样迭代:
for(auto it=B.begin();it!=B.end();it++) {
cout<<(it->first);
}
它会给我们 4、2、6 还是钥匙可以按任何顺序出现?
来自cplusplus.com page about the begin
member function of unordered_map
(link):
Notice that an unordered_map object makes no guarantees on which specific element is considered its first element.
所以不,不能保证元素将按照插入的顺序迭代。
仅供参考,您可以更简单地迭代 unordered_map
:
for (auto& it: B) {
// Do stuff
cout << it.first;
}
信息添加到 provided by @Aimery,
-
Unordered map is an associative container that contains key-value pairs with unique keys. Search, insertion, and removal of elements
have average constant-time complexity.
Internally, the elements are not sorted in any particular order but organized into buckets. Which bucket an element is placed into
depends entirely on the hash of its key. This allows fast access to
individual elements since once the hash is computed, it refers to the
exact bucket the element is placed into.
See the ref. from https://en.cppreference.com/w/cpp/container/unordered_map.
- 根据Sumod Mathilakath在知乎上的回答
If you prefer to keep intermediate data in sorted order, use std::map<key,value>
instead std::unordered_map
. It will sort on key by default using std::less<>
so you will get result in ascending order.
std::unordered_map
is an implementation of hash table data structure, so it will arrange the elements internally according to the hash value using by std::unordered_map
. But in case std::map
it is usually a red black binary tree implementation.
See the ref. from What will be order of key in unordered_map in c++ and why?.
所以,我想我们更清楚地得到了答案。
在 unordered_map 中以特定顺序插入的键是否会在使用迭代器遍历地图时以 相同的顺序 出现?
例如:如果我们在 B 中插入 (4,3), (2, 5), (6, 7)。 并像这样迭代:
for(auto it=B.begin();it!=B.end();it++) {
cout<<(it->first);
}
它会给我们 4、2、6 还是钥匙可以按任何顺序出现?
来自cplusplus.com page about the begin
member function of unordered_map
(link):
Notice that an unordered_map object makes no guarantees on which specific element is considered its first element.
所以不,不能保证元素将按照插入的顺序迭代。
仅供参考,您可以更简单地迭代 unordered_map
:
for (auto& it: B) {
// Do stuff
cout << it.first;
}
信息添加到
-
Unordered map is an associative container that contains key-value pairs with unique keys. Search, insertion, and removal of elements have average constant-time complexity.
Internally, the elements are not sorted in any particular order but organized into buckets. Which bucket an element is placed into depends entirely on the hash of its key. This allows fast access to individual elements since once the hash is computed, it refers to the exact bucket the element is placed into.
See the ref. from https://en.cppreference.com/w/cpp/container/unordered_map.
- 根据Sumod Mathilakath在知乎上的回答
If you prefer to keep intermediate data in sorted order, use
std::map<key,value>
insteadstd::unordered_map
. It will sort on key by default usingstd::less<>
so you will get result in ascending order.std::unordered_map
is an implementation of hash table data structure, so it will arrange the elements internally according to the hash value using bystd::unordered_map
. But in casestd::map
it is usually a red black binary tree implementation.See the ref. from What will be order of key in unordered_map in c++ and why?.
所以,我想我们更清楚地得到了答案。