HOWTO: STL Sample For [list::merge] Function(s)

Last reviewed: October 9, 1997
Article ID: Q168045
The information in this article applies to:
  • The Standard C++ Library included with: - Microsoft Visual C++, 32-bit Editions, version 4.2

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.

MORE INFORMATION

Required Header

   <list>
   <iostream>

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 list<T>s.

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 <list>
   #include <iostream>

   #if _MSC_VER > 1020  // later than VC++ 4.2
   using namespace std;  // std C++ libs 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;
   }

Program Output

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

REFERENCES

Visual C++ Books On Line: Visual C++ Books:C/C++:Standard C++ Library Reference.


Additional query words: STL STLSample [list::merge]
Keywords : STLIss kbcode
Version : WINNT:4.2;
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 9, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.