 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
vector::resize() vs vector::reserve() in C++
Vectors have the ability to resize itself automatically like dynamic arrays when an element is inserted or deleted, the container handle their storage automatically.
The main difference between vector resize() and vector reserve() is that resize() is used to change the size of vector where reserve() doesn't. reserve() is only used to store at least the number of the specified elements without having to reallocate memory. But in resize() if the number is smaller than the current number then it resizes the memory and deletes the excess space over it.
C++ vector::resize()
The resize() is used to change the actual number of elements in the vector. If increased, new elements are added (default-initialized). If decreased, elements are removed.
Pseudo Code
Following is the pseudocode to the vector::resize() in C++:
Begin Declare a variable v of vector type. Declare another variable it as iterator of vector type. Declare another two variable c and i to the ineger datatype. while (TRUE) do print "1.Size of the Vector". print "2.Insert Element into the Vector". print "3.Resize the vector". print "4.Display by Iterator". print "5.Exit". print "Enter your Choice:". Enter the value of variable c. Switch(c) Case 1. Print "Size of Vector:". Call size() function to print the size of the vector. Break. Case 2. Print "Enter value to be inserted:". Enter the value of variable i. Call push_back() function to input the values in the vector. Break. Case 3. Print "Resize the vector elements:". Call resize() function to resize the vector. Break. Case 4. Print "Displaying Vector by Iterator:". for (it = v.begin(); it != v.end(); it++) print the value of iterator it. Break. case 5. call exit() function to take exit. break. Default. Print "Wrong choice". End.
Example
In this example, we initialize a vector with predefined values, choose an option to insert a new element, resizes the vector, displays its contents using an iterator, and exits after execution.
#include<iostream> #include<vector> using namespace std; int main() { // Initialized vector with values vector<int> v = {10, 20, 30}; vector<int>::iterator it; // Predefined choice and input value int c = 2, i = 40; while (true) { cout<< "1. Size of the Vector"<<endl; cout<< "2. Insert Element into the Vector"<<endl; cout<< "3. Resize the vector"<<endl; cout<< "4. Display by Iterator"<<endl; cout<< "5. Exit"<<endl; // Using predefined value cout<<"Enter your Choice: "<<c<<endl; switch (c) { case 1: cout<<"Size of Vector: "<<v.size()<<endl; break; case 2: cout<<"Enter value to be inserted: "<<i<<endl; v.push_back(i); break; case 3: cout<<"Resize the vector elements:"<<endl; // Adjust vector size v.resize(5); break; case 4: cout<<"Displaying Vector by Iterator: "; for (it = v.begin(); it != v.end(); it++) { cout<<*it<<" "; } cout<<endl; break; case 5: // Proper exit return 0; default: cout<<"Wrong Choice"<<endl; } // Exit after running once c = 5; } return 0; }  Following is the output to the above program:
1. Size of the Vector 2. Insert Element into the Vector 3. Resize the vector 4. Display by Iterator 5. Exit Enter your Choice: 2 Enter value to be inserted: 40 1. Size of the Vector 2. Insert Element into the Vector 3. Resize the vector 4. Display by Iterator 5. Exit Enter your Choice: 5
C++ vector::reserve()
The Vector reserve() indicates that the vector is created such that it can store at least the number of the specified elements without reallocating memory.
Pseudo Code
Following is the pseudocode to the vector::reverse() in C++:
Begin Declare a variable v of vector type. Declare another variable it as iterator of vector type. Declare another two variable c and i to the ineger datatype. while (1) do print "1.Size of the Vector". print "2.Insert Element into the Vector". print "3.Reserve the vector". print "4.Display by Iterator". print "5.Exit". print "Enter your Choice:". Enter the value of variable c. Switch(c) Case 1. Print "Size of Vector:". Call size() function to print the size of the vector. Break. Case 2. Print "Enter value to be inserted:". Enter the value of variable i. Call push_back() function to input the values in the vector. Break. Case 3. Print "Reserve the vector elements:". Call reserve() function to reserve the size of the vector. Break. Case 4. Print "Displaying Vector by Iterator:". for (it = v.begin(); it != v.end(); it++) print the value of iterator it. Break. case 5. call exit() function to take exit. break. Default. Print "Wrong choice". End.
Example
In this example, we initialize a vector with predefined values, select an option to display its contents, and runs a loop to show menu options while updating choices before exiting.
#include<iostream> #include<vector> using namespace std; int main() { vector<int>v = {10, 20, 30, 40, 50}; // Predefined vector values vector<int>::iterator it; int c = 4; // Automatically selecting an option (Displaying elements) int i = 60; // Move the predefined inserted value outside switch for proper scope while (true) { cout<<"1. Size of the Vector"<< endl; cout<<"2. Insert Element into the Vector"<<endl; cout<<"3. Reserve the vector"<<endl; cout<<"4. Display by Iterator"<<endl; cout<<"5. Exit"<<endl; cout<<"Enter your Choice: "<<c<<endl; // No user input switch (c) { case 1: cout<<"Size of Vector: "<<v.size()<<endl; break; case 2: cout<<"Enter value to be inserted: "<<i<<endl; v.push_back(i); break; case 3: cout<<"Reserve the vector elements."<<endl; v.reserve(100); break; case 4: cout<<"Displaying Vector by Iterator: "; for (it = v.begin(); it != v.end(); it++) { cout<<*it<<" "; } cout<<endl; break; case 5: return 0; default: cout<<"Wrong Choice"<<endl; } c = 5; // Exit after execution } return 0; }  Following is the output to the above program:
1. Size of the Vector 2. Insert Element into the Vector 3. Reserve the vector 4. Display by Iterator 5. Exit Enter your Choice: 4 Displaying Vector by Iterator: 10 20 30 40 50 1. Size of the Vector 2. Insert Element into the Vector 3. Reserve the vector 4. Display by Iterator 5. Exit Enter your Choice: 5
