Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std::map auto-resize when too many elements are inserted?

Does the STL's std::map auto-resize when too many elements are inserted? If so, what's the rate?

As I understand, it should have one, but I can't find anything for resize().

If std::map does resize, how can one adjust the resize rate?

like image 236
jiafu Avatar asked Mar 12 '26 06:03

jiafu


2 Answers

Maps grow a node at a time as elements are added... you can't "resize" them like a vector - grabbing a contiguous chunk of memory earlier instead of many small chunks later - because they don't use contiguous chunks like that anyway. They have pointers linking individual nodes, with one element per node.

like image 171
Tony Delroy Avatar answered Mar 14 '26 19:03

Tony Delroy


All STL containers automatically "resize" when you insert elements. I.e. when you use some_container::insert functionality to insert elements into a container, you don't have to do anyhting else, regardless of how many elements you insert (until you reach some physical limit, like run out of memory or hit container::max_size()).

You also meantion "adjusting resize rate"... It is not possible to adjust "resize rate" even in those contanters to which this concept is applicable (like in case of std::vector). And resize method (even when it exists) does not adjust "resize rate". A container's growth strategy is an implementation detail, not exposed to the user and not controllable by the user.

Meanwhile, a typical implementation of std::map will allocate storage for each map element individually. Such container does not have and does not need any "resize rate". Its resize strategy is fixed as "add one more element every time one more element is needed" or "add N more elements every N one more elements are needed".

like image 37
AnT Avatar answered Mar 14 '26 20:03

AnT