Does std::vector::insert reserve by definition?

Humam Helfawi

When calling the insert member function on a std::vector, will it reserve before "pushing back" the new items? I mean does the standard guarantee that or not?

In other words, should I do it like this:

std::vector<int> a{1,2,3,4,5};
std::vector<int> b{6,7,8,9,10};
a.insert(a.end(),b.begin(),b.end());

or like this:

std::vector<int> a{1,2,3,4,5};
std::vector<int> b{6,7,8,9,10};
a.reserve(a.size()+b.size());
a.insert(a.end(),b.begin(),b.end());

or another better approach?

dkg

Regarding the complexity of the function [link]:

Linear on the number of elements inserted (copy/move construction) plus the number of elements after position (moving).

Additionally, if InputIterator in the range insert (3) is not at least of a forward iterator category (i.e., just an input iterator) the new capacity cannot be determined beforehand and the insertion incurs in additional logarithmic complexity in size (reallocations).

Hence, there is two cases :

  • The new capacity can be determined, therefore you won't need to call reserve
  • The new capacity can't be determined, hence a call to reserve should be useful.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How does std::vector::reserve actually work?

From Dev

Undefined behavior for std::vector reserve()

From Dev

Is std::vector::reserve(0); legal?

From Dev

reserve() Implementation for std::vector in STL

From Dev

How reserve in std::vector works + Accessing vector with []

From Dev

What does it mean to call std::vector::reserve with no arguments, and why do so after creating an empty vector?

From Dev

Does a vector assignment invalidate the `reserve`?

From Dev

Does std::vector<Object> reserve method need a copy constructor for Object class?

From Dev

Why does std::vector::insert need to copy assign?

From Dev

Why does Microsoft std::vector::insert use rotate()?

From Dev

How does overload resolution work for std::vector<int>::insert

From Dev

Thread safety std::vector push_back and reserve

From Dev

How do I reserve memory for a std::vector at construction time?

From Dev

Thread safety std::vector push_back and reserve

From Dev

How do I reserve memory for a std::vector at construction time?

From Dev

std::sort fail to work when input vector has called reserve

From Dev

Why fill_n() does not work with vector.reserve()?

From Dev

Circular dependency and std::vector::insert

From Dev

std::vector::insert vs std::list::operator[]

From Dev

Can std::vector capacity/size/reserve be used to manually manage vector memory allocation?

From Dev

std::sort order is from greatest to least when std::vector::reserve is used?

From Dev

Why does std::vector::insert invalidate all iterators after the insertion point

From Dev

Vector of vectors, reserve

From Dev

reserve and then assign to a std::string

From Dev

How is std::vector insert implemented? C++

From Java

Insert or push_back to end of a std::vector?

From Dev

Insert element into std::vector<void **(void*)>

From Dev

Insert to std::vector of string, list pair

From Dev

Compiler does not deduce template parameters (map std::vector -> std::vector)

Related Related

  1. 1

    How does std::vector::reserve actually work?

  2. 2

    Undefined behavior for std::vector reserve()

  3. 3

    Is std::vector::reserve(0); legal?

  4. 4

    reserve() Implementation for std::vector in STL

  5. 5

    How reserve in std::vector works + Accessing vector with []

  6. 6

    What does it mean to call std::vector::reserve with no arguments, and why do so after creating an empty vector?

  7. 7

    Does a vector assignment invalidate the `reserve`?

  8. 8

    Does std::vector<Object> reserve method need a copy constructor for Object class?

  9. 9

    Why does std::vector::insert need to copy assign?

  10. 10

    Why does Microsoft std::vector::insert use rotate()?

  11. 11

    How does overload resolution work for std::vector<int>::insert

  12. 12

    Thread safety std::vector push_back and reserve

  13. 13

    How do I reserve memory for a std::vector at construction time?

  14. 14

    Thread safety std::vector push_back and reserve

  15. 15

    How do I reserve memory for a std::vector at construction time?

  16. 16

    std::sort fail to work when input vector has called reserve

  17. 17

    Why fill_n() does not work with vector.reserve()?

  18. 18

    Circular dependency and std::vector::insert

  19. 19

    std::vector::insert vs std::list::operator[]

  20. 20

    Can std::vector capacity/size/reserve be used to manually manage vector memory allocation?

  21. 21

    std::sort order is from greatest to least when std::vector::reserve is used?

  22. 22

    Why does std::vector::insert invalidate all iterators after the insertion point

  23. 23

    Vector of vectors, reserve

  24. 24

    reserve and then assign to a std::string

  25. 25

    How is std::vector insert implemented? C++

  26. 26

    Insert or push_back to end of a std::vector?

  27. 27

    Insert element into std::vector<void **(void*)>

  28. 28

    Insert to std::vector of string, list pair

  29. 29

    Compiler does not deduce template parameters (map std::vector -> std::vector)

HotTag

Archive