Forward List in C++ | Set 2 (Manipulating Functions)
Last Updated :
21 Jun, 2022
Forward List in C++ | Set 1 (Introduction and Important Functions) More functions are discussed in this article Some of the operations other than insertions and deletions that can be used in forward lists are as follows :
1. merge() :- This function is used to merge one forward list with other. If both the lists are sorted then the resultant list returned is also sorted.
2. operator "=" :- This operator copies one forward list into other. The copy made in this case is deep copy.
CPP
// C++ code to demonstrate the working of
// merge() and operator=
#include<iostream>
#include<forward_list>
using namespace std;
int main()
{
// Initializing 1st forward list
forward_list<int> flist1 = {1, 2, 3};
// Declaring 2nd forward list
forward_list<int> flist2;
// Creating deep copy using "="
flist2 = flist1;
// Displaying flist2
cout << "The contents of 2nd forward list"
" after copy are : ";
for (int &x : flist2)
cout << x << " ";
cout << endl;
// Using merge() to merge both list in 1
flist1.merge(flist2);
// Displaying merged forward list
// Prints sorted list
cout << "The contents of forward list "
"after merge are : ";
for (int &x : flist1)
cout << x << " ";
cout << endl;
return 0;
}
Output:
The contents of 2nd forward list after copy are : 1 2 3
The contents of forward list after merge are : 1 1 2 2 3 3
Time Complexity: O(1)
Auxiliary Space: O(1)
3. sort() :- This function is used to sort the forward list.
4. unique() :- This function deletes the multiple occurrences of a number and returns a forward list with unique elements. The forward list should be sorted for this function to execute successfully.
CPP
// C++ code to demonstrate the working of
// sort() and unique()
#include<iostream>
#include<forward_list> // for sort() and unique()
using namespace std;
int main()
{
// Initializing 1st forward list
forward_list<int> flist1 = {1, 2, 3, 2, 3, 3, 1};
// Sorting the forward list using sort()
flist1.sort();
// Displaying sorted forward list
cout << "The contents of forward list after "
"sorting are : ";
for (int &x : flist1)
cout << x << " ";
cout << endl;
// Use of unique() to remove repeated occurrences
flist1.unique();
// Displaying forward list after using unique()
cout << "The contents of forward list after "
"unique operation are : ";
for (int &x : flist1)
cout << x << " ";
cout << endl;
return 0;
}
Output:
The contents of forward list after sorting are : 1 1 2 2 3 3 3
The contents of forward list after unique operation are : 1 2 3
Time Complexity: O(1)
Auxiliary Space: O(1)
5. reverse() :- This function is used to reverse the forward list.
6. swap() :- This function swaps the content of one forward list with other.
CPP
// C++ code to demonstrate the working of
// reverse() and swap()
#include<iostream>
#include<forward_list> // for reverse() and swap()
using namespace std;
int main()
{
// Initializing 1st forward list
forward_list<int> flist1 = {1, 2, 3,};
// Initializing 2nd forward list
forward_list<int> flist2 = {4, 5, 6};
// Using reverse() to reverse 1st forward list
flist1.reverse();
// Displaying reversed forward list
cout << "The contents of forward list after"
" reversing are : ";
for (int &x : flist1)
cout << x << " ";
cout << endl << endl;
// Displaying forward list before swapping
cout << "The contents of 1st forward list "
"before swapping are : ";
for (int &x : flist1)
cout << x << " ";
cout << endl;
cout << "The contents of 2nd forward list "
"before swapping are : ";
for (int &x : flist2)
cout << x << " ";
cout << endl;
// Use of swap() to swap the list
flist1.swap(flist2);
// Displaying forward list after swapping
cout << "The contents of 1st forward list "
"after swapping are : ";
for (int &x : flist1)
cout << x << " ";
cout << endl;
cout << "The contents of 2nd forward list "
"after swapping are : ";
for (int &x : flist2)
cout << x << " ";
cout << endl;
return 0;
}
Output:
The contents of forward list after reversing are : 3 2 1
The contents of 1st forward list before swapping are : 3 2 1
The contents of 2nd forward list before swapping are : 4 5 6
The contents of 1st forward list after swapping are : 4 5 6
The contents of 2nd forward list after swapping are : 3 2 1
Time Complexity: O(1)
Auxiliary Space: O(1)
7. clear() :- This function clears the contents of forward list. After this function, the forward list becomes empty.
8. empty() :- This function returns true if the list is empty otherwise false.
CPP
// C++ code to demonstrate the working of
// clear() and empty()
#include<iostream>
#include<forward_list> // for clear() and empty()
using namespace std;
int main()
{
// Initializing forward list
forward_list<int> flist1 = {1, 2, 3,};
// Displaying forward list before clearing
cout << "The contents of forward list are : ";
for (int &x : flist1)
cout << x << " ";
cout << endl;
// Using clear() to clear the forward list
flist1.clear();
// Displaying list after clear() performed
cout << "The contents of forward list after "
<< "clearing are : ";
for (int &x : flist1)
cout << x << " ";
cout << endl;
// Checking if list is empty
flist1.empty() ? cout << "Forward list is empty" :
cout << "Forward list is not empty";
return 0;
}
Output:
The contents of forward list are : 1 2 3
The contents of forward list after clearing are :
Forward list is empty
Time Complexity: O(1)
Auxiliary Space: O(1)
Recent articles on forward_list
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read
C++ Standard Template Library (STL) The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith
9 min read
Map in C++ STL In C++, maps are associative containers that store data in the form of key value pairs sorted on the basis of keys. No two mapped values can have the same keys. By default, it stores data in ascending order of the keys, but this can be changes as per requirement.Example:C++#include <bits/stdc++.h
8 min read