list merge() function in C++ STL - GeeksforGeeks

19 Jul, 2018
The list::merge() is an inbuilt function in C++ STL which merges two sorted lists into one. The lists should be sorted in ascending order. If no comparator is passed in parameter, then it merges two sorted lists into a single sorted list. If a comparator is passed in the parameter, then it merges the list accordingly doing internal comparisons.
Syntax:list1_name.merge(list2_name)
Parameters: The function accepts a single mandatory parameter list2_name which specifies the list to be merged into list1.Return value: The function does not returns anythingProgram below demonstrates the function:Program 1:// program below demonstrates the// merge function in c++#include <bits/stdc++.h>using namespace std;  int main(){    // declaring the lists    // initially sorted    list<int> list1 = { 10, 20, 30 };    list<int> list2 = { 40, 50, 60 };      // merge operation    list2.merge(list1);      cout << "List:  ";      for (auto it = list2.begin(); it != list2.end(); ++it)        cout << *it << " ";      return 0;}Output:List:  10 20 30 40 50 60
Syntax:list1_name.merge(list2_name, comparator)
Parameters: The function accepts two parameters which are described below:list2-name – It specifies the list2 which is to be merged in list1.comparator – It is a binary predicate which takes two values of the same type that of those contained in the list, returns true if the first argument is considered to go before the second in the strict weak ordering it defines, and false otherwise.Return value: The function does not returns anything// program below demonstrates the// merge function in c++#include <bits/stdc++.h>using namespace std;  // comparator which compares elements internallybool comparator(int first, int second){    return first < second;}int main(){    // declaring the lists    list<int> list1 = { 1, 70, 80 };    list<int> list2 = { 2, 3, 4 };      // merge operation    list1.merge(list2, comparator);      cout << "List: ";      for (auto it = list1.begin(); it != list1.end(); ++it)        cout << *it << " ";      return 0;}Output:List: 1 2 3 4 70 80

Syntax:list1_name.merge(list2_name)
Parameters: The function accepts a single mandatory parameter list2_name which specifies the list to be merged into list1.Return value: The function does not returns anythingProgram below demonstrates the function:Program 1:// program below demonstrates the// merge function in c++#include <bits/stdc++.h>using namespace std;  int main(){    // declaring the lists    // initially sorted    list<int> list1 = { 10, 20, 30 };    list<int> list2 = { 40, 50, 60 };      // merge operation    list2.merge(list1);      cout << "List:  ";      for (auto it = list2.begin(); it != list2.end(); ++it)        cout << *it << " ";      return 0;}Output:List:  10 20 30 40 50 60

list1_name.merge(list2_name)

Parameters: The function accepts a single mandatory parameter list2_name which specifies the list to be merged into list1.
Return value: The function does not returns anything
Program below demonstrates the function:
Program 1:
// program below demonstrates the// merge function in c++#include <bits/stdc++.h>using namespace std;  int main(){    // declaring the lists    // initially sorted    list<int> list1 = { 10, 20, 30 };    list<int> list2 = { 40, 50, 60 };      // merge operation    list2.merge(list1);      cout << "List:  ";      for (auto it = list2.begin(); it != list2.end(); ++it)        cout << *it << " ";      return 0;}
List:  10 20 30 40 50 60

Syntax:list1_name.merge(list2_name, comparator)
Parameters: The function accepts two parameters which are described below:list2-name – It specifies the list2 which is to be merged in list1.comparator – It is a binary predicate which takes two values of the same type that of those contained in the list, returns true if the first argument is considered to go before the second in the strict weak ordering it defines, and false otherwise.Return value: The function does not returns anything// program below demonstrates the// merge function in c++#include <bits/stdc++.h>using namespace std;  // comparator which compares elements internallybool comparator(int first, int second){    return first < second;}int main(){    // declaring the lists    list<int> list1 = { 1, 70, 80 };    list<int> list2 = { 2, 3, 4 };      // merge operation    list1.merge(list2, comparator);      cout << "List: ";      for (auto it = list1.begin(); it != list1.end(); ++it)        cout << *it << " ";      return 0;}Output:List: 1 2 3 4 70 80

list1_name.merge(list2_name, comparator)

Parameters: The function accepts two parameters which are described below:
list2-name – It specifies the list2 which is to be merged in list1.
comparator – It is a binary predicate which takes two values of the same type that of those contained in the list, returns true if the first argument is considered to go before the second in the strict weak ordering it defines, and false otherwise.
Return value: The function does not returns anything
// program below demonstrates the// merge function in c++#include <bits/stdc++.h>using namespace std;  // comparator which compares elements internallybool comparator(int first, int second){    return first < second;}int main(){    // declaring the lists    list<int> list1 = { 1, 70, 80 };    list<int> list2 = { 2, 3, 4 };      // merge operation    list1.merge(list2, comparator);      cout << "List: ";      for (auto it = list1.begin(); it != list1.end(); ++it)        cout << *it << " ";      return 0;}
List: 1 2 3 4 70 80

CPP-Functions
cpp-list
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in C++
C++ Classes and Objects
Virtual Function in C++
Operator Overloading in C++
Templates in C++ with Examples
Constructors in C++
vector erase() and clear() in C++
Socket Programming in C/C++
Object Oriented Programming in C++
Substring in C++