forward_list merge() in C++ STL

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of forward_list::merge() function in C++.

What is a Forward_list in STL?

Forward list are sequence containers that allow constant time insert and erase operations anywhere within the sequence. Forward list are implement as a singly-linked lists. The ordering is kept by the association to each element of a link to the next element in the sequence.

What is forward_list::merge()?

forward_list::merge() is an inbuilt function in C++ STL which is declared in header file. merge() is used to merge two sorted forward_list into one. 

Before merging two lists we must ensure the lists are in sorted order. If there is no comparator is passed then it merges two lists into one sorted list. When we also want internal comparisons between two lists then we must add comparator.

Syntax

flist_container1.merge(flist_container2); //will merge both lists
flist_container1.merge(flist_container2, comparator);

This function can accept either one or two parameters −

Parameters

  • list_container2 − This is a the object of the second list which is to be merged

  • comparator − This defines an internal comparison. This is a binary predicate which containes two inputs of the value same as defined in the list container, it returns true if the list_container1 element is considered to go before the list_container2, else it will be false.

Return Value

This function returns nothing.

Example

/* In the below code, we are creating the two forward lists and both are sorted and the task is to merge them using merge() function in C++ which should be sorted */

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   //creating forward list by inserting sorted values
   forward_list<int> myForwardList1 = { 1, 3, 5, 7 };
   forward_list<int> myForwardList2 = { 2, 4, 6 };
   // merging two sorted forward lists
   myForwardList1.merge(myForwardList2);
   cout << "elements after merging" << endl;
   for (auto i = myForwardList1.begin(); i != myForwardList1.end(); ++i)
      cout << *i << " ";
   return 0;
}

Output

If we run the above code it will generate the following output

elements after merging
1 2 3 4 5 6 7

With comparator

Example

/* In the below code, we are creating the two forward lists and both are unsorted and the task is to sort the list first and then merge them using merge() function in C++ and that should be sorted. */

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main (){
   //create unsorted forward list
   forward_list<int> myForwardList1 = {3, 2, 9};
   forward_list<int> myForwardList2 = {8, 1, 2};
   //sorting the forward list using sort() function
   myForwardList1.sort();
   myForwardList2.sort();
   myForwardList1.merge(myForwardList2);
   cout << "Elements after merging" << endl;
   for (auto i = myForwardList1.begin(); i != myForwardList1.end(); ++i)
      cout << *i << " ";
   return 0;
}

Output

If we run the above code it will generate the following output

Elements after merging
1 2 2 3 8 9
raja
Published on 02-Mar-2020 11:49:54
Advertisements