C++ List merge()

C++ List merge() function merges two sorted list in an increasing order. It merges y list into the given list container and this removes all the elements from y.

Two condition can be ocurred in merge function():

If the comparator is not passed in the parameter then the two sorted list is merged into a single one.

If the comparator is passed in the parameter then the list is merged based on their internal comparisons.

Syntax

Consider two list list1 and list2. Syntax would be:

Parameter

list2: The list to be merged with the list1.

compare: It is a compare function object that compares the value of the first argument with the second argument. If the value of the first argument is less than the second argument then it returns true otherwise false.

Return value

It does not return any value.

Example 1

Let's see a simple example

Output:

1 2 3 4 5 6 7 8

In this example, merge() function merges the list li with the list li1 in a single list.

Example 2

Let's see a simple example when the comparator is passed in the parameter

Output:

5 6 7 9 10 11 15

In this example, merge() function merges the list based on the internal comparisons.





Latest Courses