Now the question is, from my first main post.
Is the unordered_map same as my idea #1 and map same as my idea #2?
( basically same )
|
unordered_map and map behave the same way, the only difference is that, when iterating over them, unordered_map's iterator doesn't access the elements in order
This is because those two are implemented differently, unordered_map is implemented as a hashmap and map as a binary tree (you don't really have to understand what this means)
I looked at the header of map but i didn't understood quite how the sorting
is working there. |
the sorting takes place automatically. When inserting an element the container places it in the correct position.
(caution: this section might confuse you)
You can choose what kind of compare-function you want to have by setting the 3rd template parameter.
The default is less which means that you'll iterate your map like this:
... -2, -1, 0, 1, 2 ... (imagine the nodes -2 to 2 exist)
You could set it to greather like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream> // std::cout, std::endl
#include <functional> // std::greater
#include <map> // std::map
int main()
{
std::map<int, int, std::greater<int>> container;
container[2] = 500;
container[0] = 10;
container[1] = 100;
for(auto& i : container)
std::cout << i.first << " => " << i.second << std::endl;
}
|
http://cpp.sh/92qr
Anything else than less doesn't seem to be a good idea in your case so I'll not go in details here.
I hope I could help you to find out the answer yourself. ;)