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

</>
Copy
// 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

ParameterDescription
otherThe 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

</>
Copy
#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:

  1. Two std::list containers, list1 and list2, are initialized with sorted values {1, 3, 5} and {2, 4, 6}, respectively.
  2. The merge function is called to merge list2 into list1, maintaining the sorted order.
  3. After the operation, list1 contains all elements in sorted order, and list2 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

</>
Copy
#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:

  1. Two std::list containers, list1 and list2, are initialized with sorted values in descending order.
  2. A custom comparison function customCompare is defined to sort elements in descending order.
  3. The merge function is called with customCompare, combining the two lists into a single list sorted in descending order.
  4. After the operation, list1 contains all elements in descending order, and list2 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

</>
Copy
#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:

  1. Two std::list containers, list1 and list2, are initialized with sorted values, including duplicates.
  2. The merge function is called to combine the two lists into list1, maintaining their sorted order.
  3. After the operation, list1 contains all elements from both lists in sorted order, including duplicates, and list2 is empty.

Output:

List 1 after merge: 1 2 2 2 3 3 4 5
List 2 after merge: