vector : : resize() in C++ STL

Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container.

vector::resize()

The function alters the container’s content in actual by inserting or deleting the elements from it. It happens so,

  • If the given value of n is less than the size at present then extra elements are demolished.
  • If n is more than current size of container then upcoming elements are appended at the end of the vector.

Syntax:

vectorname.resize(int n, int val)

    Parameters:

  • n – it is new container size, expressed in number of elements.
  • val – if this parameter is specified then new elements are initialized with this value.
  • Return value:

  • This function do not returns anything.
  • Exception:

  • The only exception if it so happens is Bad_alloc thrown, if reallocation fails.

Below programs illustrate the working of the function

  1. Size of the vector container is lowered.
    [sourcecode language=”CPP”]
    // resizing of the vector
    #include <iostream>
    #include <vector>

    using namespace std;

    int main()
    {
    vector<int> vec;

    // 5 elements are inserted
    // in the vector
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(5);

    cout << "Contents of vector before resizing:"
    << endl;

    // displaying the contents of the
    // vector before resizing
    for (int i = 0; i < vec.size(); i++)
    cout << vec[i] << " ";

    cout << endl;

    // vector is resized
    vec.resize(4);

    cout << "Contents of vector after resizing:"
    << endl;

    // displaying the contents of the
    // vector after resizing
    for (int i = 0; i < vec.size(); i++)
    cout << vec[i] << " ";

    return 0;
    }

    [/sourcecode]
    Output:

    Contents of the vector before resizing:
    1 2 3 4 5 
    Contents of the vector after resizing:
    1 2 3 4 
    
  2. Size of the vector container is increased.
    [sourcecode language=”CPP”]
    // resizing of the vector
    #include <iostream>
    #include <vector>

    using namespace std;

    int main()
    {
    vector<int> vec;

    // 5 elements are inserted
    // in the vector
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(5);

    cout << "Contents of vector before resizing:"
    << endl;

    // displaying the contents of the
    // vector before resizing
    for (int i = 0; i < vec.size(); i++)
    cout << vec[i] << " ";

    cout << endl;

    // vector is resized
    vec.resize(8);

    cout << "Contents of vector after resizing:"
    << endl;

    // displaying the contents of
    // the vector after resizing
    for (int i = 0; i < vec.size(); i++)
    cout << vec[i] << " ";

    return 0;
    }

    [/sourcecode]
    Output:

    Contents of the vector before resizing:
    1 2 3 4 5 
    Contents of the vector after resizing:
    1 2 3 4 5 0 0 0 
    
  3. Size of the vector container is increased and new elements are initialized with specified value.
    [sourcecode language=”CPP”]
    // resizing of the vector
    #include <iostream>
    #include <vector>

    using namespace std;

    int main()
    {
    vector<int> vec;

    // 5 elements are inserted
    // in the vector
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(5);

    cout << "Contents of vector before resizing:"
    << endl;

    // displaying the contents of
    // the vector before resizing
    for (int i = 0; i < vec.size(); i++)
    cout << vec[i] << " ";

    cout << endl;

    // vector is resized
    vec.resize(12, 9);

    cout << "Contents of vector after resizing:"
    << endl;

    // displaying the contents
    // of the vector after resizing
    for (int i = 0; i < vec.size(); i++)
    cout << vec[i] << " ";

    return 0;
    }

    [/sourcecode]
    Output:

    Contents of the vector before resizing:
    1 2 3 4 5 
    Contents of the vector after resizing:
    1 2 3 4 5 9 9 9 9 9 9 9 
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.