C++ Forward_list Library - resize() Function


Advertisements

Description

The C++ function std::forward_list::resize() changes the size of forward_list. If n is smaller than current size then extra elements are destroyed. If n is greater than current container size then new elements are inserted at the end of forward_list.

Declaration

Following is the declaration for std::forward_list::resize() function form std::forward_list header.

C++11

void resize (size_type n);

Parameters

n − Number of element to be inserted.

Return value

None

Exceptions

If reallocation fails then bad_alloc exception is thrown.

Time complexity

Linear i.e. O(n)

Example

The following example shows the usage of std::forward_list::resize() function.

#include <iostream>
#include <forward_list>

using namespace std;

bool foo(int n) {
   return (n > 5);
}

int main(void) {

   forward_list<int> fl;

   fl.resize(5);

   cout << "List contents after resize operation" << endl;

   for (auto it = fl.begin(); it != fl.end(); ++it)
      cout << *it << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

List contents after resize operation
0
0
0
0
0
forward_list.htm
Advertisements