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:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
forward_list< int > fl = { 10, 20, 30, 40, 50 };
cout << "The contents of forward list :" ;
for ( auto it = fl.begin(); it != fl.end(); ++it)
cout << *it << " " ;
cout << endl;
fl.resize(7);
cout << "The contents of forward list :" ;
for ( auto it = fl.begin(); it != fl.end(); ++it)
cout << *it << " " ;
return 0;
}
|
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:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
forward_list< int > fl = { 10, 20, 30, 40, 50 };
cout << "The contents of forward list :" ;
for ( auto it = fl.begin(); it != fl.end(); ++it)
cout << *it << " " ;
cout << endl;
fl.resize(3);
cout << "The contents of forward list :" ;
for ( auto it = fl.begin(); it != fl.end(); ++it)
cout << *it << " " ;
return 0;
}
|
Output:
The contents of forward list :10 20 30 40 50
The contents of forward list :10 20 30
Time Complexity: O(n)
Auxiliary Space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
20 Jun, 2022
Like Article
Save Article