C++ list merge() Function
Example
Merge two sorted lists:
list<int> numbers = {1, 3, 5};
list<int> more = {2, 4, 6};
numbers.merge(more);
for (int number : numbers) {
cout << number << "\n";
}
Definition and Usage
The merge() function moves all the elements from another list into this list. Both lists must be sorted before you call merge(), and the result is also sorted.
The elements are moved, not copied, so the other list is empty after the merge.
The merge function is defined in the <list> header file.
Tip: If the lists are not sorted, use sort() on both lists first.
Syntax
list.merge(list<type> other);
<type> refers to the type of the data that the list contains.
Parameter Values
| Parameter | Description |
|---|---|
| other | Required. A sorted list with the same type of data. It is empty after the merge. |
Technical Details
| Returns: | Nothing (void). |
|---|
Related Pages
Read more about lists in our List Tutorial.