list merge() function in C++ STL

C++Server Side ProgrammingProgramming

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

What is a List in STL?

List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.

What is list::merge()?

list::merge() is an inbuilt function in C++ STL which is declared in <list> header file. merge() is used to merge two lists into a one. We can simply just merge the two lists, or if we want extra comparisons we can also add a comparator.

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

list_container1.merge(list_container2); //will merge both lists in list_container1
list_container1.merge(list_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.

Without comparator

Example

In the below code we are creating two sorted lists and the task is to merge the lists and the resulting output should also be sorted.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //creating the sorted list
   list<int> myList_1 = {2, 4, 6, 8 };
   list<int> myList_2 = {1, 3, 5, 7 };
   //using merge() function to merge the lists
   myList_2.merge(myList_1);
   cout <<"Lists after merging : ";
   for(auto i = myList_2.begin(); i != myList_2.end(); ++i)
      cout << *i << " ";
   return 0;
}

Example

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

Lists after merging : 1 2 3 4 5 6 7 8

With comparator

Example

In the below code we have to merge the two lists and then sort the generated list as a final output.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
bool compare(int myList_1, int myList_2){
   return ( int(myList_1)<int(myList_2) );
}
int main(){
   //create a list
   list<int> myList_1 = {2, 4, 1 };
   list<int> myList_2 = {7, 6, 5 };
   myList_1.sort();
   myList_2.sort();
   //using merge() function to merge the lists
   myList_1.merge(myList_2);
   myList_2.push_back (3);
   myList_1.merge(myList_2,compare);
   cout<<"List Elements are : ";
   for(auto i = myList_1.begin(); i!=myList_1.end(); ++i)
      cout<< ' ' << *i;
   return 0;
}

Output

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

List Elements are : 1 2 3 4 5 6 7
raja
Published on 02-Mar-2020 09:24:27
Advertisements