Home C++ Algorithms Functions merge() C++ std::merge() Function Definition In C++, std::merge() takes two sorted ranges and writes all their elements into a destination in sorted order. Nothing is removed, so duplicates are kept.
This is the same idea as the merge step in merge sort. It is fast because each source is read only once.
Syntax Add #include <algorithm>, then call std::merge(). Start with these two lines.
std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));
std::merge(a.begin(), a.end(), b.begin(), b.end(), out.begin(), comp); Call What it does std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));Merge two sorted vectors into a new one. std::merge(a.begin(), a.end(), b.begin(), b.end(), out.begin(), comp);Merge with your own ordering.
Library declarations template<class InputIt1, class InputIt2, class OutputIt>
OutputIt merge(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first); Parameters Parameter Description first1, last1 Required. The first sorted range. first2, last2 Required. The second sorted range. d_first Required. Where the merged result is written. comp Optional. The comparison that both ranges were sorted with.
Return Value Return Value Description Iterator Points just after the last element written in the destination.
C++ Examples 1. Merge two vectors 2. Duplicates are kept 3. Descending order Example 1 — Merge two vectors Combine two sorted lists.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> a = {1, 3, 5};
std::vector<int> b = {2, 4, 6};
std::vector<int> out;
std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));
for (int x : out) std::cout << x << ' ';
std::cout << '\n';
return 0;
} How It Works At each step merge() compares the front value of each list and writes the smaller one. The values alternate between a and b here: 1, 2, 3, 4, 5, 6. The result is sorted without calling sort(). Example 2 — Duplicates are kept Equal values from both lists all appear in the result.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> a = {1, 2, 2};
std::vector<int> b = {2, 3};
std::vector<int> out;
std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));
for (int x : out) std::cout << x << ' ';
std::cout << '\n';
return 0;
} How It Works a holds two 2s and b holds one.merge() never drops values, so all three 2s are written.Use a set operation like set_union() if you want to remove the repeats. Example 3 — Descending order Pass std::greater for lists sorted from large to small.
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> a = {9, 5, 1};
std::vector<int> b = {8, 4};
std::vector<int> out;
std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out),
std::greater<int>());
for (int x : out) std::cout << x << ' ';
std::cout << '\n';
return 0;
} How It Works Both lists are sorted from big to small. std::greater<int>() tells merge() to use that order.The merged list also goes from big to small. Frequently Asked Questions What does std::merge() do? In C++, std::merge() merges two sorted ranges into one sorted range. Include <algorithm> to use it.
Which header and C++ version do I need for std::merge()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::merge() return? It returns an iterator to the position after the last element written in the destination.
What happens if the inputs are not sorted? The result is not sorted either. Sort both inputs first.
How is merge() different from inplace_merge()? merge() writes to a separate destination from two ranges. inplace_merge() merges two sorted halves inside one range.
Next: min() Returns the smaller of two values, or the smallest in a list.
min() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
10 people found this page helpful
Helpful Share Copy link Suggestion