Benefits of using reserve() in a vector - C++

后端 未结 7 1372
感动是毒
感动是毒 2020-12-15 04:13

What is the benefit of using reserve when dealing with vectors. When should I use them? Couldn\'t find a clear cut answer on this but I assume it is faster when you reserve

7条回答
  •  悲哀的现实
    2020-12-15 05:05

    Don't know about people smarter than you, but I would say that you should call reserve in advance if you are going to perform lots in insertion operations and you already know or can estimate the total number of elements, at least the order of magnitude. It can save you a lot of reallocations in good circumstances.

提交回复
热议问题