Javatpoint Logo
Javatpoint Logo

forward_list merge() in C++

In this article, we will discuss the forward_list merge() function in C++ with its syntax, example.

A forward list is a sequence container that allows constant time insert and erase operations wherever in the sequence. Forward lists are created using singly linked lists. The ordering is maintained by associating each element with a link to the following element in the sequence.

What is the function forward_list::merge()?

The forward_list::merge() is a built-in C++ STL function that is declared in the header file. It can merge two sorted forward_lists into one using the merge() function. In other words, the merge() function is used to combine two sorted forward_lists into a single one.

We need to verify that the two lists are in order of sort before combining them. If no comparator is provided, it combines two lists into a single sorted list. When we want to do internal evaluations between two lists, we must provide a comparator.

Syntax:

It has the following syntax:

This function accepts one or two arguments:

list_contain2: It represents the object of the second list that will be merged.

Comparator: It specifies an internal comparison. It is a binary predicate with two inputs of the same value that are specified in the list container; it returns true if the list_contain1 element has been determined to come before the list_contain2 element. Otherwise, it returns false.

Example:

Let us take a C++ program to illustrate the use of forward_list merge() function.

Output:

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

Example 2:

Let us take another C++ program to illustrate the use of forward_list merge() function.

Output:

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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA