Skip to content
Related Articles

Related Articles

forward_list resize() function in C++ STL
  • Difficulty Level : Basic
  • Last Updated : 31 Aug, 2018

The forward_list::resize() is an inbuilt function in C++ STL which changes the size of forward_list. If the given size is greater than the current size then new elements are inserted at the end of the forward_list. If the given size is smaller than current size then extra elements are destroyed.

Syntax:

forwardlist_name.resize(n)

Parameter: The function accepts only one mandatory parameter n which specifies the new size of the forward list.

Return value: The function does not return anything.

Below programs illustrates the above function:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// forward_list::resize() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    forward_list<int> fl = { 10, 20, 30, 40, 50 };
  
    // Prints the forward list elements
    cout << "The contents of forward list :";
    for (auto it = fl.begin(); it != fl.end(); ++it)
        cout << *it << " ";
  
    cout << endl;
  
    // resize to 7
    fl.resize(7);
  
    // // Prints the forward list elements after resize()
    cout << "The contents of forward list :";
    for (auto it = fl.begin(); it != fl.end(); ++it)
        cout << *it << " ";
  
    return 0;
}

chevron_right


Output:

The contents of forward list :10 20 30 40 50 
The contents of forward list :10 20 30 40 50 0 0

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// forward_list::resize() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    forward_list<int> fl = { 10, 20, 30, 40, 50 };
  
    // Prints the forward list elements
    cout << "The contents of forward list :";
    for (auto it = fl.begin(); it != fl.end(); ++it)
        cout << *it << " ";
  
    cout << endl;
  
    // resize to 3
    fl.resize(3);
  
    // Prints the forward list elements after resize()
    cout << "The contents of forward list :";
    for (auto it = fl.begin(); it != fl.end(); ++it)
        cout << *it << " ";
  
    return 0;
}

chevron_right


Output:

The contents of forward list :10 20 30 40 50 
The contents of forward list :10 20 30

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :