Forward List Merge In C++ - C++ Programming Tutorial
C++ Course / Dynamic Programming / Forward List Merge In C++

Forward List Merge In C++

BLUF: Mastering Forward List Merge In C++ 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 Merge In C++

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

In this guide, we will explore the merge function for forward_list in C++ along with its syntax and an illustration.

A forward list is a type of sequence container that enables quick insertion and deletion operations at any point within the sequence. These lists are generated using singly linked lists, where each element is linked to the subsequent element to preserve the order.

What is the function forward_list::merge?

The forwardlist::merge function is a predefined feature in the C++ STL library, specified in the header file. This function is designed to combine two sorted forwardlists into a single list by utilizing the merge operation. Essentially, merge facilitates the merging of two sorted forward_lists into a unified list.

We must ensure that the order of sorting is correct for both lists before merging them. In cases where a comparator is not specified, the two lists are merged into a unified sorted list. If there is a need for internal comparisons between the two lists, a comparator must be supplied.

Syntax:

It has the following syntax:

Example

flist_contain1.merge(flist_contain2); //merging lists
flist_contain1.merge(flist_contain2, comparator);

This function accepts one or two arguments:

list_contain2: It denotes the element from the second list that is to be combined.

It defines an internal comparison mechanism using a binary predicate that evaluates two inputs of equal value within the list container. The comparator returns true if the listcontain1 element is identified to precede the listcontain2 element, otherwise, it returns false.

Example:

Let's consider a C++ code example to demonstrate the functionality of the forward_list merge method.

Example

// CPP application demonstrating the forward_list::merge() method 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	forward_list<int> flist1 = { 11, 35, 45, 67 }; 
	forward_list<int> flist2 = { 30, 40, 50 }; 

	// merging of two lists
	flist1.merge(flist2); 

	// display result
	cout << "The elements of the list are:" << endl; 
	for (auto ite = flist1.begin(); ite != flist1.end(); ++ite) 
		cout << *ite << " "; 

	return 0; 
}

Output:

Output

The elements of the list are:
11 30 35 40 45 50 67

Example 2:

Let's consider a different C++ program to demonstrate how the forward_list merge method is employed.

Example

// CPP application demonstrating the forward_list::merge() method 
#include <bits/stdc++.h> 
using namespace std; 
// function for comparing
bool cmp_fun(int a, int b) 
{ 
	return a > b; 
} 

int main() 
{ 
	forward_list<int> flist1 = { 20, 24, 38, 45 }; 
	forward_list<int> flist2 = { 21, 39, 11 }; 

	// merging the two lists
	flist1.merge(flist2, cmp_fun); 

	//displaying the result
	cout << "The elements of the lists are:" << endl; 
	for (auto ite = flist1.begin(); ite != flist1.end(); ++ite) 
		cout << *ite << " "; 

	return 0; 
}

Output:

Output

The elements of the lists are:
21 39 20 24 38 45 11

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