partial_sort_copy

将源范围的元素添加到源元素由小于或另一指定的二进制谓词排序的目标范围。

template<class InputIterator, class RandomAccessIterator>
   RandomAccessIterator partial_sort_copy(
      InputIterator _First1, 
      InputIterator _Last1,
      RandomAccessIterator _First2, 
      RandomAccessIterator _Last2
   );
template<class InputIterator, class RandomAccessIterator, class BinaryPredicate>
   RandomAccessIterator partial_sort_copy(
      InputIterator _First1, 
      InputIterator _Last1,
      RandomAccessIterator _First2, 
      RandomAccessIterator _Last2,
      BinaryPredicate _Comp
   );

参数

  • _First1
    解决输入的迭代器第一个元素的位置位于源范围。

  • _Last1
    解决输入的迭代器通过最终元素的一位置位于源范围。

  • _First2
    解决一个随机访问迭代器第一个元素的位置在已排序的目标范围。

  • _Last2
    解决一个随机访问迭代器通过最终元素的位置一在已排序的目标范围。

  • _Comp
    定义要满足条件的用户定义的谓词函数对象,如果两个组件要执行视为等效的。二进制谓词采用两个参数并返回 true,在满足和 false,在未满足。

返回值

解决一个随机访问迭代器在目标区域的位置的元素在从源范围插入的最后一个元素之外。

备注

源和目标范围不能重叠并且必须是有效的;所有指针必须dereferenceable,在每个序列中最后位置必须是可访问的从开始按增量。

二进制谓词必须提供可靠弱顺序,以便不是等效的元素进行排序,但是,等效元素不是。如果没有小于另一个,两个元素是相当于在小于下,相比,但不一定相等。

示例

// alg_partial_sort_copy.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <iostream>

int main() {
    using namespace std;
    vector<int> v1, v2;
    list<int> list1;
    vector<int>::iterator iter1, iter2;
    list<int>::iterator list1_Iter, list1_inIter;

    int i;
    for (i = 0; i <= 9; i++)
        v1.push_back(i);

    random_shuffle(v1.begin(), v1.end());

    list1.push_back(60);
    list1.push_back(50);
    list1.push_back(20);
    list1.push_back(30);
    list1.push_back(40);
    list1.push_back(10);

    cout << "Vector v1 = ( " ;
    for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
        cout << *iter1 << " ";
    cout << ")" << endl;

    cout << "List list1 = ( " ;
    for (list1_Iter = list1.begin();
         list1_Iter!= list1.end();
         list1_Iter++)
        cout << *list1_Iter << " ";
    cout << ")" << endl;

    // Copying a partially sorted copy of list1 into v1
    vector<int>::iterator result1;
    result1 = partial_sort_copy(list1.begin(), list1.end(),
             v1.begin(), v1.begin() + 3);

    cout << "List list1 Vector v1 = ( " ;
    for (iter1 = v1.begin() ; iter1 != v1.end() ; iter1++)
        cout << *iter1 << " ";
    cout << ")" << endl;
    cout << "The first v1 element one position beyond"
         << "\n the last L 1 element inserted was " << *result1
         << "." << endl;

    // Copying a partially sorted copy of list1 into v2
    int ii;
    for (ii = 0; ii <= 9; ii++)
        v2.push_back(ii);

    random_shuffle(v2.begin(), v2.end());
    vector<int>::iterator result2;
    result2 = partial_sort_copy(list1.begin(), list1.end(),
             v2.begin(), v2.begin() + 6);

    cout << "List list1 into Vector v2 = ( " ;
    for (iter2 = v2.begin() ; iter2 != v2.end(); iter2++)
        cout << *iter2 << " ";
    cout << ")" << endl;
    cout << "The first v2 element one position beyond"
         << "\n the last L 1 element inserted was " << *result2
         << "." << endl;
}

示例输出

Vector v1 = ( 8 1 9 2 0 5 7 3 4 6 )
List list1 = ( 60 50 20 30 40 10 )
List list1 Vector v1 = ( 10 20 30 2 0 5 7 3 4 6 )
The first v1 element one position beyond
 the last L 1 element inserted was 2.
List list1 into Vector v2 = ( 10 20 30 40 50 60 1 8 5 2 )
The first v2 element one position beyond
 the last L 1 element inserted was 1.

要求

标头: <algorithm>

命名空间: std

请参见

参考

partial_sort_copy (STL Samples)

Predicate Version of partial_sort_copy

标准模板库