C++ std::list::merge
The std::list::merge
function merges two sorted std::list
containers into one. It combines the elements from the target list and another list, maintaining their sorted order. Optionally, you can provide a custom comparison function to define the sorting criteria. After the operation, the source list becomes empty.
Syntax of std::list::merge
// 1. Merge with default comparison (operator<)
void merge(std::list<T>& other);
// 2. Merge with custom comparison function
template <class Compare>
void merge(std::list<T>& other, Compare comp);
Parameters
Parameter | Description |
---|---|
other | The std::list container to merge into the current list. This list must be sorted. |
comp | (Optional) A binary comparison function that returns true if the first argument should precede the second. It defines the sorting order of the merged list. |
Return Value
This function does not return a value. It modifies the target list by merging it with the source list. After the operation, the source list is empty.
Exceptions
The std::list::merge
function does not throw exceptions unless the comparison function throws an exception. It provides strong exception safety: if an exception is thrown, neither list is modified.
Examples for std::list::merge
Example 1: Merging Two Sorted Lists with Default Comparison
This example demonstrates how to use merge
to combine two sorted lists:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> list1 = {1, 3, 5};
std::list<int> list2 = {2, 4, 6};
// Merge list2 into list1
list1.merge(list2);
std::cout << "List 1 after merge: ";
for (const auto& elem : list1) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::cout << "List 2 after merge: ";
for (const auto& elem : list2) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- Two
std::list
containers,list1
andlist2
, are initialized with sorted values{1, 3, 5}
and{2, 4, 6}
, respectively. - The
merge
function is called to mergelist2
intolist1
, maintaining the sorted order. - After the operation,
list1
contains all elements in sorted order, andlist2
is empty.
Output:
List 1 after merge: 1 2 3 4 5 6
List 2 after merge:
Example 2: Merging with a Custom Comparison
This example demonstrates how to merge two lists using a custom comparison function:
Program
#include <iostream>
#include <list>
// Custom comparison function: Sort in descending order
bool customCompare(int a, int b) {
return a > b;
}
int main() {
std::list<int> list1 = {5, 3, 1};
std::list<int> list2 = {6, 4, 2};
// Merge list2 into list1 using custom comparison
list1.merge(list2, customCompare);
std::cout << "List 1 after merge: ";
for (const auto& elem : list1) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::cout << "List 2 after merge: ";
for (const auto& elem : list2) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- Two
std::list
containers,list1
andlist2
, are initialized with sorted values in descending order. - A custom comparison function
customCompare
is defined to sort elements in descending order. - The
merge
function is called withcustomCompare
, combining the two lists into a single list sorted in descending order. - After the operation,
list1
contains all elements in descending order, andlist2
is empty.
Output:
List 1 after merge: 6 5 4 3 2 1
List 2 after merge:
Example 3: Merging Lists with Duplicate Elements
This example demonstrates how merge
handles lists with duplicate elements:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> list1 = {1, 2, 2, 3};
std::list<int> list2 = {2, 3, 4, 5};
// Merge list2 into list1
list1.merge(list2);
std::cout << "List 1 after merge: ";
for (const auto& elem : list1) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::cout << "List 2 after merge: ";
for (const auto& elem : list2) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- Two
std::list
containers,list1
andlist2
, are initialized with sorted values, including duplicates. - The
merge
function is called to combine the two lists intolist1
, maintaining their sorted order. - After the operation,
list1
contains all elements from both lists in sorted order, including duplicates, andlist2
is empty.
Output:
List 1 after merge: 1 2 2 2 3 3 4 5
List 2 after merge: