Merge Operation

In this lesson, we'll learn different ways of combining ranges.

Merge operations empower us to merge sorted ranges in a new sorted range. The merge algorithm requires that the ranges and the algorithm use the same sorting criteria. If not, the program is undefined. Per default, the predefined sorting criterion std::less is used. If we use our sorting criterion, it has to obey the strict weak ordering. If not, the program is undefined.

We can merge two sorted ranges with std::inplace_merge and std::merge. We can check with std::includes if one sorted range is in another sorted range. We can merge two sorted ranges with std::set_difference, std::set_intersection, std::set_symmetric_difference and std::set_union merge two sorted ranges in a new sorted range.

inplace_merge: Merges two sorted ranges [first, mid] and [mid, last] in place.

Get hands-on with 1200+ tech skills courses.