Any proposal is just a proposal, to try and measure something. We cannot tell you the most effective way to do the insertion; you must measure your own specific use case and see which is best.
If your map is compact and dense (almost all elements from the 0 - max key are occupied by real data), and the maximum key is small enough to be a reasonable index of the array, you can switch to using std::vector<value>
and always insert at the end. Since it grows, you will have to redistribute the vector (usually this is when the vector doubles). It can be expensive, but usually the insert will be very cheap. You do not need to deal with the potential rebalancing of the binary tree, and the vector is extremely convenient for other purposes.
If your key card space is not compact / dense, and the maximum key is so large that its inconceivable index in memory, then a hint insert will be your best choice.
If the order doesn't matter, you can try std :: unordered_map . This is a hash table implementation. Thus, the cost of insertion will be related to the quality and speed of the hash. It should be trivial and quick to take your 64-bit key and turn it into a hash file size_t (size_t may even be 64 bits).
But do not take a word for him, measure it and see for yourself ...
source share