algorithm - STL C++

inplace_merge()

Declaration

template <class BidirIterator>
void inplace_merge(
	BidirIterator xFirst,
	BidirIterator xMiddle,
	BidirIterator xLast
);

Description

This function merges the ordered range of entries starting at "xFirst" up to the one before "xMiddle" with the ordered range of entries starting at "xMiddle" up to the one before "xLast." The result is that the entries in these two ranges is now ordered in the range from "xFirst" up to the one before "xLast." In the overloaded version, the comparison function "xComp" is used to define the ordering.

Header Include

#include <algorithm>

Overloads

template <class BidirIterator, class BinaryPred>
void inplace_merge(
	BidirIterator xFirst,
	BidirIterator xMiddle,
	BidirIterator xLast,
	BinaryPred xComp
);

Example

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
	using namespace std;

	// Create a vector instance
	vector<int> qV;
	// First five entries
	for (int i = 0; i < 10; i += 2) {
		qV.push_back(i);
	}
	// Last four entries
	for (int i = -2; i < 9; i += 3) {
		qV.push_back(i);
	}

	vector<int>::iterator qIter;
	cout << "Original Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;

	// Merge the first five entries with the last four.
	inplace_merge(qV.begin(), qV.begin() + 5, qV.end());

	cout << "Merged Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;

	// Keep the window open
	cin.get();
	return 0;
}

Output

inplace_merge() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.