Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

C++ STL: forward_list (Complete Guide)

Divya Jain
Divya Jain

Forward_list in STL is a sequential container. It is very similar to list in STL but the list stores both the location of the previous and the next elements of the list while forward_list only stores the location of the next element, thus it saves memory. Insertion and deletion are efficient in forward_list and take only constant time. It is implemented as a singly LinkedList in memory. Forward_lists are included in #include <forward_list> header file.

Forward_list Declaration

Syntax:

forward_list<data_type> name;

data_type: data type of the elements to be stored in the list.

Example:

forward_list<int> l; //initializes a forward list of size 0 which stores integer values 

Forward_list Initialization

forward_list<data_type> name={initial_values};

initial_values: optional parameter which initializes the list with the given values.

Example:

forward_list<int> l = {1,2,3,4,5}; //initializes a forward list of size 5 with values 1,2,3,4,5 

Functions on forward_list

  • push_front(element): It adds a new element to the front of the list. 
    Parameters: the element to be inserted
    Return value: void
     
  • pop_front(): It deletes the first element of the list. 
    Parameters: None
    Return value: void
     
  • insert_after(iterator,{values}): It inserts the values after the given position. 
    Parameters: iterator pointing to the position after which the value needs to be inserted, values that need to be inserted Return value: iterator, pointing to the last inserted element
     
  • erase_after(iterator): It deletes the values after the given position. 
    Parameters: iterator, pointing to the position after which the value needs to be deleted
    Return value: void
     
  • remove(element): It removes all the occurrences of the given element. 
    Parameters: element that needs to be deleted
    Return value: void
     
  • front(): It returns the first element of the list. 
    Parameters: None 
    Return value: void
     
  • begin(): It returns an iterator pointing to the first element of the list. 
    Parameters: None 
    Return value: iterator, pointing to the first element
     
  • end(): It returns an iterator pointing to the last element of the list. 
    Parameters: None 
    Return value: iterator, pointing to the last element
     
  • max_size(): It returns the maximum number of elements that can be stored by the forward_list. 
    Parameters: None 
    Return value: integer, the maximum size
     
  • empty(): It tells us whether the list is empty or not
    Parameters: None
    Return value: boolean, true if the list is empty otherwise false.
     
  • reverse(): It reverses the list. 
    Parameters: None
    Return value: void
#include<iostream>
#include<forward_list>
using namespace std;

void print(forward_list <int> l)
{
	forward_list<int>::iterator itr;
	for(itr=l.begin();itr!=l.end();++itr)
		cout<<*itr<<' ';
	cout<<'\n';
}

int main()
{
	forward_list<int> l={1,2,3};
	for(int i=0;i<5;++i)
	{
		l.push_front(i);
	}
	print(l);
	cout<<"First element " <<l.front()<<'\n';
	l.pop_front();
	l.insert_after(l.begin(),{5,6,7});
	print(l);
	l.erase_after(l.begin());
	print(l);
	l.remove(2);
	cout<<"List after removing all occurrences of 2: ";
	print(l);
	if(!l.empty()){
	cout<<"Max size of list "<<l.max_size()<<'\n';
	}
	return 0;
}

Output

4 3 2 1 0 1 2 3 
First element 4
3 5 6 7 2 1 0 1 2 3 
3 6 7 2 1 0 1 2 3 
List after removing all occurrences of 2: 3 6 7 1 0 1 3 
Max size of list 576460752303423487
Divya Jain
Divya Jain
Divya is an incoming SDE at Atlassian. She loves solving problems and web development. She loves to explore new things and is up for interesting conversations about tech.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet