Is it guaranteed that when a hash_map/unordered_map is loaded with the same items, they will have the same order when iterated? Basically I have a hashmap which I load from a file, and from which I periodically feed a limited number of items to a routine, after which I free the hashmap. After the items are consumed, I re-load the same file to the hashmap and want to get the next batch of items after the point where I stopped the previous time. The point at which I stop would be identified by the key.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Technically no, they are not guaranteed to be in any particular order.
In practice however, given that you use deterministic hash function, what you want to do should be fine.
consider
you can reasonably expect that name-value pairs in
map1
andmap2
go in the same order.No, you cannot safely do this. Firstly, it's not guaranteed by the standard, but even if you ignore the standard and look at real implementations, it is a bad idea.
Most hash table structures are not history independent. That is: the state of the hash table depends not only on what items it contains, but also what order they were inserted.
Here is a concrete example:
When I build this using clang and the libc++ implementation of the C++ standard library, I get the following output:
Note that the order is different in every case. This is not at all unusual for hash tables.