Article ID: 168045
Article Last Modified on 1/9/2006
<list> <iostream>
void merge(list& x);NOTE: The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.
//////////////////////////////////////////////////////////////////////
//
// Compile options needed: -GX
//
// merge.cpp : This example show how to merge two sorted list<T>s.
//
// Functions:
//
// list::merge
//
// Written by Andrew Bradnan
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////
#include <list>
#include <iostream>
using namespace std;
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
typedef list<int, allocator<int> > LISTINT;
void main()
{
// List's must be sorted first
int list1[] = {12,11,10,7,6,5,3,1};
int list2[] = {22,21,20,10,7,6,5};
LISTINT test1;
LISTINT test2;
LISTINT::iterator i;
test1.insert (test1.begin(), list1, list1 + 8);
test2.insert (test2.begin(), list2, list2 + 7);
test1.merge (test2);
// 22 21 20 12 11 10 10 7 7 6 6 5 5 3 1
for (i = test1.begin(); i != test1.end(); ++i)
cout << *i << " ";
cout << endl;
}
12 11 10 7 6 5 3 1 22 21 20 10 7 6 5
Additional query words: STL STLSample [list::merge]
Keywords: kbhowto kbcode KB168045