merge (STL Samples)

在 Visual C++ 演示如何使用 合并 标准 (STL)模板库函数。

template<
   class InputIterator1,
   class InputIterator2,
   class OutputIterator
> inline
   OutputIterator merge(
      InputIterator1 First1,
      InputIterator1 Last1,
      InputIterator2 First2,
      InputIterator2 Last2,
      OutputIterator Result 
   )

备注

备注

类/参数名在原型不匹配版本在头文件。修改某些提高可读性。

合并 算法将两个排序顺序:[First1。Last1) 和 [First2。Last2) 到开始 结果单个排序序列。 此版本,假设范围 [First1。Last1) 和 [First2。Last2使用 operatorAMP_LT,) 排序。 如果两个范围包含相等的值,首先要存储从第一个范围的值。 合并重叠范围的结果是未定义的。

示例

// merge.cpp
// compile with: /EHsc
//
// Functions:
//    merge : Merge two sorted sequences
//            into a single sorted list.

// disable warning C4786: symbol greater than 255 character,
// okay to ignore
#pragma warning(disable: 4786)

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <deque>

using namespace std ;

int main()
{
    const int MAX_ELEMENTS = 8 ;

    // Define a template class vector of int
    typedef vector<int> IntVector ;

    //Define an iterator for template class vector of ints
    typedef IntVector::iterator IntVectorIt ;

    IntVector NumbersVector(MAX_ELEMENTS) ;

    IntVectorIt startv, endv, itv ;

    // Define a template class list of int
    typedef list<int> IntList ;

    //Define an iterator for template class list of ints
    typedef IntList::iterator IntListIt ;

    IntList NumbersList ;

    IntListIt first, last, itl ;

    // Define a template class deque of int
    typedef deque<int> IntDeque ;

    //Define an iterator for template class deque of ints
    typedef IntDeque::iterator IntDequeIt ;

    IntDeque NumbersDeque(2 * MAX_ELEMENTS) ;

    IntDequeIt itd ;

    // Initialize vector NumbersVector
    NumbersVector[0] = 4 ;
    NumbersVector[1] = 10;
    NumbersVector[2] = 70 ;
    NumbersVector[3] = 10 ;
    NumbersVector[4] = 30 ;
    NumbersVector[5] = 69 ;
    NumbersVector[6] = 96 ;
    NumbersVector[7] = 100;

    startv = NumbersVector.begin() ;   // location of first
                                       // element of NumbersVector

    endv = NumbersVector.end() ;  // one past the location
                                  // last element of NumbersVector

    // sort NumbersVector, merge requires the sequences
    // to be sorted
    sort(startv, endv) ;

    // print content of NumbersVector
    cout << "NumbersVector { " ;
    for(itv = startv; itv != endv; itv++)
        cout << *itv << " " ;
    cout << " }\n" << endl ;

    // Initialize vector NumbersList
    for(int i = 0; i < MAX_ELEMENTS; i++)
        NumbersList.push_back(i) ;

    first = NumbersList.begin() ;   // location of first
                                     // element of NumbersList

    last = NumbersList.end() ;  // one past the location
                                // last element of NumbersList

    // print content of NumbersList
    cout << "NumbersList { " ;
    for(itl = first; itl != last; itl++)
        cout << *itl << " " ;
    cout << " }\n" << endl ;

    // merge the elements of NumbersVector
    // and NumbersList and place the
    // results in NumbersDeque
    merge(startv, endv, first, last, NumbersDeque.begin()) ;

    cout << "After calling merge\n" << endl ;

    // print content of NumbersDeque
    cout << "NumbersDeque { " ;
    for(itd = NumbersDeque.begin();
        itd != NumbersDeque.end(); itd++)
        cout << *itd << " " ;
    cout << " }\n" << endl ;
}

Output

NumbersVector { 4 10 10 30 69 70 96 100  }

NumbersList { 0 1 2 3 4 5 6 7  }

After calling merge

NumbersDeque { 0 1 2 3 4 4 5 6 7 10 10 30 69 70 96 100  }

要求

**标题:**algorithm

请参见

概念

标准模板库示例