Forward List In C++ Manipulating Functions - C++ Programming Tutorial
C++ Course / Dynamic Programming / Forward List In C++ Manipulating Functions

Forward List In C++ Manipulating Functions

BLUF: Mastering Forward List In C++ Manipulating Functions is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Forward List In C++ Manipulating Functions

C++ is renowned for its efficiency. Learn how Forward List In C++ Manipulating Functions enables low-level control and high-performance computing in the tutorial below.

This guide is focused on presenting the Standard Template Library in C++ programming, showcasing the use of various manipulation functions.

Given the extensive nature of the C++ Standard Template Library (STL), comparable to a vast ocean, we will now explore several essential functions including merge, the assignment operator "=", sort, unique, reverse, as well as the swap operator. Each function is accompanied by its corresponding code snippet and the resulting outputs.

Important functions 1: merge and Operator "=. "

C++ code

Example

//below, we are writing down the C++ programming language code to demonstrate
//the concept of forwarding List in C++ manipulating functions along with the 
//working of merge() and operator=
#include<iostream>
#include<forward_list>
using namespace std;

int main() {	

	//the below code snippet initialises the 1st forwarding list for us
	forward_list<int> f_list_1 = {51, 52, 53};
	

	//the below code snippet initialises the 2nd forwarding list for us
	forward_list<int> f_list_2;
	
	// the below code snippet creates the deep copy using the equal to 
	// operator "=" for us
	f_list_2 = f_list_1;
	
	//the below code snippet Displays f_list_2
	cout << "displaying the content we have at 2nd forward list."
			" After performing the copy operations, we have are: ";
	for (int &x : f_list_2)
		cout << x << " ";
	cout << endl;
	
	//the below code snippet Uses merge() function to merge both list in 1
	f_list_1.merge(f_list_2);
	
	//the below code snippet displays 
	// merged forward list for us
	// it also prints the sorted list for us
	cout << "displaying the content we have at forwarding list."
			" After performing the merge operations, we have are: ";
	for (int &x : f_list_1)
		cout << x << " ";
	cout << endl;
	
	return 0;	
}

Output:

Output

displaying the content have at the 2nd forward list after performing the copy operations we have are: 51 52 53 
showing the content have at the forwarding list after completing the merge operations we have are: 51 51 52 52 53 53

Important Functions 2: sort and Unique

C++ code

Example

//below, we are writing down the C++ programming language code to 
//demonstrate
//the concept of forwarding List in C++ manipulating functions along with 
//the working of
// sort() and unique()
#include<iostream>
#include<forward_list> 
using namespace std;

int main()
{
	
	forward_list<int> f_list_1 = {51, 72, 93, 2, 93, 93, 51};

	
	f_list_1.sort();

	cout << "displaying our content at the forward list."
			" After performing the sort operations, we have are";
	for (int &x : f_list_1)
		cout << x << " ";
	cout << endl;

	f_list_1.unique();

	cout << "displaying our content at the forward list."
			" After performing the unique operations, we have are";
	for (int &x : f_list_1)
		cout << x << " ";
	cout << endl;

	return 0;
}

Output:

Output

displaying the content, we have on the forward list. After performing the sort operations, we have 51 51 72 93 93 93 
displaying the content, we have on the forward list. After completing the unique functions, we have 51 72 93

Important Functions 3: swap

C++ code

Example

//below, we are writing down the C++ programming language code to 
//demonstrate
//the concept of forwarding List in C++ manipulating functions along with 
//the working of
// reverse() and swap()
#include<iostream>
#include<forward_list> 
using namespace std;
int main()
{
	//the below code snippet initialises the 1st forwarding list for us
	forward_list<int> f_list_1 = {31, 62, 73,};

	//the below code snippet initialises the 2nd forwarding list for us
	forward_list<int> f_list_2 = {54, 65, 86};

// the below code snippet uses the reverse() operator to reverse 1st forward list
//for us
 	f_list_1.reverse();

// the below code snippet displays the reverse forward list
	cout << "The contents of the forward list after."
			" reversing are: ";
	for (int &x : f_list_1)
		cout << x << " ";
	cout << endl << endl;

	// Displaying forward list before swapping
	cout << "The contents of 1st forward list."
			"before swapping are: ";
	for (int &x : f_list_1)
		cout << x << " ";
	cout << endl;
	cout << "The contents of 2nd forward list."
			"before swapping are: ";
	for (int &x : f_list_2)
		cout << x << " ";
	cout << endl;

	//here we are using of swap() to swap the list
	f_list_1.swap(f_list_2);

	//here, we are displaying the forward list after swapping
	cout << "content of 1st forward list "
			"After swapping are: ";
	for (int &x : f_list_1)
		cout << x << " ";
	cout << endl;

	cout << "content of 2nd forward list."
			"After swapping are: ";
	for (int &x : f_list_2)
		cout << x << " ";
	cout << endl;

	return 0;
}

Output:

Output

The contents of the forwarding list after reversing are: 73 62 31 
The contents of 1st forward list before swapping are: 73 62 31 
The contents of the 2nd forward list before changing are: 54 65 86 
range of 1st forward list after changing are: 54 65 86 
range of 2nd forward list after changing is: 73 62 31

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience