C++ std::inplace_merge() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::inplace_merge() takes one range made of two sorted parts that sit next to each other, and merges them into one sorted range.

You tell it where the first part ends with a middle iterator. It is the key step of a merge sort.

Syntax

Add #include <algorithm>, then call std::inplace_merge(). Start with these two lines.

C++
std::inplace_merge(v.begin(), v.begin() + 3, v.end());
std::inplace_merge(v.begin(), mid, v.end(), comp);
CallWhat it does
std::inplace_merge(v.begin(), v.begin() + 3, v.end());Merge the part before index 3 with the part from index 3.
std::inplace_merge(v.begin(), mid, v.end(), comp);Merge with your own ordering.
Library declarations
C++
template<class BidirIt>
void inplace_merge(BidirIt first, BidirIt middle, BidirIt last);

template<class BidirIt, class Compare>
void inplace_merge(BidirIt first, BidirIt middle, BidirIt last, Compare comp);

Parameters

ParameterDescription
first, lastRequired. The whole range.
middleRequired. Where the second sorted part begins. [first, middle) and [middle, last) must each be sorted.
compOptional. The comparison both parts were sorted with.

Return Value

Return ValueDescription
voidNothing is returned. The range is sorted in place.

C++ Examples

Example 1 — Merge two halves

A vector has two sorted halves.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 4, 7, 2, 3, 9};

    std::inplace_merge(v.begin(), v.begin() + 3, v.end());

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. The first half is 1 4 7 and the second half is 2 3 9. Both are sorted.
  2. v.begin() + 3 tells the function where the second half begins.
  3. Merging them gives one sorted vector.

Example 2 — Merge words

Merge two sorted groups of strings.

C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> v = {"ant", "cat", "bee", "dog"};

    std::inplace_merge(v.begin(), v.begin() + 2, v.end());

    for (const std::string& x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. ant cat and bee dog are both in alphabetical order.
  2. The function takes the smaller front word each time.
  3. The final list is alphabetical.

Example 3 — Append, then merge

Add a sorted list to the end, then merge.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {2, 5, 8};

    v.insert(v.end(), {1, 6, 9});
    std::inplace_merge(v.begin(), v.begin() + 3, v.end());

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. insert() adds the sorted values 1, 6 and 9 behind the first three.
  2. The middle marker stays at index 3, where the old data ended.
  3. inplace_merge() interleaves both parts into order.

Frequently Asked Questions

In C++, std::inplace_merge() merges two consecutive sorted parts of one range in place. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns nothing (void). The two sorted parts are merged into one sorted range.
It is the merge step in merge sort and works well when you append a sorted batch to already sorted data.
merge() writes to a different destination. inplace_merge() works inside one range.

Next: is_heap()

Checks whether a range is a heap.

is_heap() 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.

19 people found this page helpful