C++ std::list merge() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::list

Definition

In C++, std::list::merge() combines two sorted lists into one sorted list. It takes the nodes of the other list and links them into the right places of this list.

No element is copied or moved in memory, and afterwards the other list is empty. The merge is stable: equal elements from this list stay before equal elements from the other list. Include <list> to use it.

This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.

Syntax

Call merge on the list that should receive the elements, and give the other list. A comparison function is optional.

C++
a.merge(b);                       // both sorted with <
a.merge(b, std::greater<int>());  // both sorted with >
CallWhat it does
a.merge(b)Moves all elements of b into a. Both lists must already be sorted in ascending order.
a.merge(b, comp)Same, but both lists must be sorted with the comparison comp, for example std::greater<int>().

If your lists are not sorted yet, call sort() on both of them first. If you only want to join them without sorting, use splice().

Library declarations
C++
void merge(list& other);
void merge(list&& other);

template<class Compare>
void merge(list& other, Compare comp);

template<class Compare>
void merge(list&& other, Compare comp);

Parameters

ParameterDescription
otherRequired. The sorted list to take the elements from. It must be a different list. It is empty after the call.
compOptional. A function that returns true when its first argument should come before its second. Both lists must be sorted with the same rule.

Return Value

Return ValueDescription
Nothingmerge() returns void. The first list holds all elements and the second list is empty.

C++ Examples

Example 1 — merge() Two Sorted Lists

Two sorted lists are joined into one sorted list. The second list ends up empty.

C++
#include <iostream>
#include <list>

int main() {
    std::list<int> a = {1, 3, 5};
    std::list<int> b = {2, 4, 6};

    a.merge(b);

    for (int n : a) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    std::cout << "b has " << b.size() << " elements\n";
    return 0;
}

How It Works

  1. List a holds 1 3 5 and list b holds 2 4 6. Both are already in ascending order.
  2. merge() walks through both lists and links each node of b in front of the first larger node of a. The result is 1 2 3 4 5 6.
  3. The nodes of b now belong to a, so b.size() is 0.

Example 2 — merge() with std::greater

If the lists are sorted from big to small, tell merge() to use the same rule.

C++
#include <functional>
#include <iostream>
#include <list>

int main() {
    std::list<int> a = {9, 5, 1};
    std::list<int> b = {8, 4, 2};

    a.merge(b, std::greater<int>());

    for (int n : a) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. Both lists are sorted from the biggest number to the smallest number.
  2. std::greater<int>() tells merge() that a bigger number comes first. Without it, merge() would expect ascending lists.
  3. The merged list stays in descending order: 9 8 5 4 2 1.

Example 3 — merge() Keeps Iterators Valid

An iterator into the second list stays valid and now points into the first list.

C++
#include <iostream>
#include <iterator>
#include <list>

int main() {
    std::list<int> a = {1, 4};
    std::list<int> b = {2, 3, 5};

    auto it = b.begin();      // points at 2
    a.merge(b);

    std::cout << "*it: " << *it << '\n';
    std::cout << "position in a: " << std::distance(a.begin(), it) << '\n';
    return 0;
}

How It Works

  1. it points at the node with 2 inside list b.
  2. merge() relinks that node into a between 1 and 4. The node is not copied, so it stays valid.
  3. *it still reads 2. std::distance(a.begin(), it) counts one step, so it is the second element of the merged list.

Frequently Asked Questions

It merges another sorted list into this sorted list. The result is sorted, and the other list becomes empty.
Yes. Both lists must be sorted with the same ordering. If they are not, the result is not sorted and a debug build may report an error.
No. It relinks the nodes of the other list into this list. Elements are not copied or moved, so iterators and references stay valid.
Yes. If an element of this list and an element of the other list are equal, the one from this list stays first.
merge() keeps the result sorted by putting each node in the right place. splice() moves nodes to the exact position you give and does not look at the values.

Next: pop_back()

Remove the last element of the list with std::list.

pop_back() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful