4 ms·
I think for C++ vector, when current underlying array gets filled, the common strategy is allocate a new array double the size of the current one, and copy cont
by captain_price7 6y ago
I think for C++ vector, when current underlying array gets filled, the common strategy is allocate a new array double the size of the current one, and copy contents over to that.
- tester34 6y agosame in C# for list
- rerx 6y agoBut if you expect an upper bound of the required size of that stay, it may be worthwhile to explicitly set the capacity of the vector to avoid these reallocations.
- spacechild1 6y agoThat's exactly the purpose of std::vector's “reserve“ method, btw.
- twoodfin 6y agoI once worked with a C++ text processing library that saw more than a 50% throughput bump just from reserve()’ing std::string and std::vector space in hot code paths.