Microsoft KB Archive/168045

= How to use the list::merge STL function in Visual C++ =

Article ID: 168045

Article Last Modified on 1/9/2006

-

APPLIES TO

 The Standard C++ Library, when used with:  Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

 Microsoft Visual C++ 2005 Express Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q168045

<div class="notice_section">

Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.

<div class="summary_section">

SUMMARY
The sample code below illustrates how to use the [list::merge] STL function(s) in Visual C++.

Note that there are some differences in the implementation of the Standard C++ Library components in Visual C++ version 4.2 versus later revisions. The relevant sections of code below compile conditionally based upon the value of _MSC_VER.

<div class="moreinformation_section">

Prototype
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.

Description
This example shows how to merge two sorted lists.

Sample Code
//////////////////////////////////////////////////////////////////////   //    // 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 #include 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 > 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; }

Program Output
12 11 10 7 6 5 3 1 22 21 20 10 7 6 5

<div class="references_section">