Detailed explanation of reserve and resize of C++ vector

      The reserve of the vector increases the capacity of the vector, but its size does not change! And resize changes the capacity of the vector and also increases its size!
The reasons are as follows:
      reserve is the space reserved by the container, but the element object is not actually created in the space, so the element in the container cannot be referenced before adding a new object. When adding a new element, the push_back()/insert() function is called.

      Resize is to change the size of the container, and after the object is created, after calling this function, you can refer to the object in the container, so when adding a new element, use the operator[] operator, or use the iterator to refer to the element object. At this time, the push_back() function is called again, which is added behind this new space.

      The parameter forms of the two functions are also different. One parameter after the reserve function is the space of the container that needs to be reserved; the resize function can have two parameters, the first parameter is the new size of the container, and the second parameter is to be added. The new element in the container, if this parameter is omitted, then the default constructor of the element object is called. The following is an example of the use of these two functions:
Example 1:
vector<int> myVec;
myVec.reserve( 100 ); // The new element has not been constructed, 
                                       // at this time the element cannot be accessed with []
for (int i = 0; i < 100; i++ )

     myVec.push_back( i ); //The new element is constructed at this time
}
myVec.resize( 102 ); // Constructs two new elements with the element's default constructor
myVec[100] = 1; // Directly manipulates the new elements
myVec[101] = 2;  
Example 2:
#include <vector>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    vector<int> vect;
    
    vect.push_back(1);
    vect.push_back(2);
    vect.push_back(3);
    vect .push_back(4);
    vect.reserve(100);
    cout<<vect.size()<<endl; //size is 4, but capacity is 100
    int i = 0;
    for (i = 0; i < 104; i++)
    {
        cout<<vect[i]<<endl;
    }
    return 0;
}
Example 3:
#include <vector>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    vector<int> vect;    
    vect.push_back(1);
    vect.push_back(2);
    vect.push_back(3);
    vect.push_back(4) ;
    vect.resize(100); //The new space does not cover the space occupied by the original four elements, now size and capacity are both 100
    cout<<vect.size()<<endl;
    int i = 0;
    for ( i = 0; i < 104; i++)
    {
        cout<<vect[i]<<endl;  
    }
    return 0;
}
Example 4:
#include <vector>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    vector<int> vect;        
    vect.resize(100); // allocate 100 spaces
    vect.push_back(1);
    vect.push_back(2);
    vect.push_back(3);
    vect.push_back(4);
    cout<<vect.size()<<endl; //The size is now 104, but the capacity is not It must be 104, and the vector will automatically increase the reserved space.
    int i = 0;
    for (i = 0; i < 104; i++)
    {
        cout<<vect[i]<<endl;  
    }
    return 0;
}
      From the above example It can be seen that whether resize or reserve is called, the two have no effect on the original elements of the container.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325167976&siteId=291194637
Recommended