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
    處理輸入的 Iterator 第一個項目的位置位於來源範圍。

  • _Last1
    處理輸入的 Iterator 超過最後一個項目的位置是在來源範圍。

  • _First2
    解決的隨機存取 Iterator 的第一個項目位置在已排序的目的範圍。

  • _Last2
    解決的隨機存取 Iterator 超過最後一個項目的位置是在已排序的目的範圍。

  • _Comp
    定義要滿足條件的使用者定義之述詞函式物件,如果兩個項目將會視為相等。一個二進位述詞採用兩個引數並傳回 true ,當內容和 false ,則內容。

傳回值

解決的隨機存取 Iterator 在目的範圍的第一個位置的項目會從來源範圍插入的最後一個項目之外。

備註

來源和目的範圍不可以重疊且必須是有效的,任何指標必須 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

標準樣板程式庫