C++ Forward_list Library - pop_front() Function



Description

The C++ function std::forward_list::pop_front() removes first element from forward_list and reduces size of forward_list by one.

Declaration

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

C++11

void pop_front();

Parameters

None

Return value

None

Exceptions

This member function never throws exception. Calling this function on empty forward_list causes undefined behavior.

Time complexity

Constant i.e. O(1)

Example

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

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl = {1, 2, 3, 4, 5};

   cout << "List contains following elements before" 
      " pop_front operation" << endl;

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

   fl.pop_front();
   fl.pop_front();

   cout << "List contains following elements after" 
      " pop_front 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 contains following elements before pop_front operation
1
2
3
4
5
List contains following elements after pop_front operation
3
4
5
forward_list.htm
Advertisements