beautypg.com

Merge ordered sequences, Chapter, 14, merge ordered sequences – HP Integrity NonStop H-Series User Manual

Page 190: Chapter 14

background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

Merge Ordered Sequences

The algorithm merge() combines two ordered sequences to form a new ordered sequence. The
size of the result is the sum of the sizes of the two argument sequences. This should be
contrasted with the set_union() operation, which eliminates elements that are duplicated in both
sets. The set_union() function will be described later in this section.

The merge operation is stable. This means, for equal elements in the two ranges, not only is the
relative ordering of values from each range preserved, but the values from the first range always
precede the elements from the second. The two ranges are described by a pair of iterators,
whereas the result is defined by a single output iterator. The arguments are shown in the
following declaration:

OutputIterator merge (InputIterator first1, InputIterator last1,
InputIterator first2, InputIterator last2,
OutputIterator result [, Compare ]);

The example program illustrates a simple merge, the use of a merge with an inserter, and the
use of a merge with an output stream iterator.

void merge_example ()
// illustrate the use of the merge algorithm
{
// make a list and vector of 10 random integers
vector aVec(10);
list aList(10, 0);
generate (aVec.begin(), aVec.end(), randomValue);
sort (aVec.begin(), aVec.end());
generate_n (aList.begin(), 10, randomValue);
aList.sort();

// merge into a vector
vector vResult (aVec.size() + aList.size());
merge (aVec.begin(), aVec.end(), aList.begin(), aList.end(),
vResult.begin());

// merge into a list
list lResult;

This manual is related to the following products: