Initializing a std::map when the size is known in advance

vanna picture vanna · Oct 24, 2012 · Viewed 39.2k times · Source

I would like to initialize a std::map. For now I am using ::insert but I feel I am wasting some computational time since I already know the size I want to allocate. Is there a way to allocate a fixed size map and then fill the map ?

Answer

Bo Persson picture Bo Persson · Oct 24, 2012

No, the members of the map are internally stored in a tree structure. There is no way to build the tree until you know the keys and values that are to be stored.