I need to store about 0.1million int into the unordered_map and need the key(int, int) to search them
The size of the data are fixed, and I don't need to change the data so oftenly
Search is much more often than insert(more than 20 times of insert)
I try to find the answer from books and google and become more confused than before
Someone said: When the data are huge, you should use map, because it is faster than vector blablabla
Someone said: Vector could be faster than unordered_map sometimes, because the memory of vector is contiguous blablabla
I don't know which side should I trust?Could anybody help me?
thanks a lot
A vector is not much of a map. Well its actually a very specialised map where the key is a limited range of contiguous integers beginning with 0. So if you know in advance that your keys will all lie with a limited range then a vector would be fastest to access. The greater the range of integer keys then the more memory that will be required for the vector. If however your keys are sparsely separated over a large range then vector become less practical and a map more memory efficient.
I would say use a vector if possible because it will *always* be as fast or faster than a map. If the vector consumes too much memory then use a map. Also an unordered_map is not always going to be faster than a standard map. That depends on your data and your hashing algorithm. If the data is quick to hash then it should be quicker than accessing a tree map (standard). But a tree will be much more memory efficient.
So if you are going with a map then use an unordered_map if you can afford the memory and its genuinely quicker than a standard map. But do some testing because I have found that std::map can be just as fast and uses less resources.