reserve() is a member function on std::vector which is used to change capacity without changing the length (filling with new elements). For example:

std::vector<int> stack{};
stack.push_back(1);
stack.push_back(2);
stack.push_back(3);

Each of these push_back calls may do reallocation to make storage available for new elements. Here, we can use reserve() function to set capacity to 10 (just a number I took) so that no more reallocation happens for at least 10 elements.

std::vector<int> stack{};
 
stack.reserve(10);
 
stack.push_back(1);
stack.push_back(2);
stack.push_back(3);
 
for (const int item : stack)
{
    std::cout << item << " "; 
} // prints 1 2 3

Unlike resize(), it only changes the capacity, it does not fill new places with values. For example,

std::vector<int> stack{};
stack.resize(10);
for (const int item : stack)
{
    std::cout << item << " "; 
} // should print 0's 10times.

References

  1. https://www.learncpp.com/cpp-tutorial/stdvector-and-stack-behavior/