0

I'm studying about vector with C++.

so, As you know from the title, I don't know why the capacity of a vector increase by a factor of 1.5.

Among many, many numbers, why is it 1.5 times?

I think it has to do with time complexity, but I don't know the exact reason.

malove
  • 1
  • 1
  • 4
    Any constant >1 works, they don't all use 1.5, some use 2. It is a compromise, bigger is faster but wastes more memory. See also https://groups.google.com/g/comp.lang.c++.moderated/c/asH_VojWKJw – Marc Glisse Feb 10 '23 at 06:57
  • It can depend on implementation I think. – kiner_shah Feb 10 '23 at 06:58
  • @MarcGlisse thks ! Ultimately, it's due to memory efficiency issues. – malove Feb 10 '23 at 08:05
  • Theoretically, what is important that the growth is a factor (and not e.g. a constant amount). This leads to insertion having amortized complexity O(1). The duplicate link discusses why a factor of around 1.5 may be preferable for memory allocation when relocating the vector storage. – nielsen Feb 10 '23 at 08:16

0 Answers0