當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Algorithm partial_sort()用法及代碼示例

C++算法partial_sort()函數用於重新排列range[first, last)中的元素,使得first和middle之間的元素排序,而middle和last之間的元素不指定順序。

第一個版本使用運算符比較元素,第二個版本使用 comp 進行比較。

用法

default (1)       template <class RandomAccessIterator>
  	         void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,
                         RandomAccessIterator last);

custom (2)      template <class RandomAccessIterator, class Compare>
                      void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,
                           RandomAccessIterator last, Compare comp);

參數

first:一個隨機訪問迭代器,指向要部分排序的範圍中的第一個元素。

last:一個隨機訪問迭代器,指向要部分排序的範圍中過去的最後一個元素。

middle: 一個隨機訪問迭代器,指向 sub-range 中要排序的最後一個元素之後的那個。

comp: 一個用戶定義的二元謂詞函數,它接受兩個參數,如果兩個參數按順序返回真,否則返回假。它遵循嚴格的弱排序來對元素進行排序。

返回值

複雜度

平均複雜度在第一個和最後一個之間的距離上小於線性。執行最多 N*log (M) 個元素比較,其中 N = last - first 和 M = middle - first。

數據競爭

範圍 [first, last) 中的對象被更改。

異常

如果任何元素比較、元素交換(或移動)或迭代器上的操作引發異常,則此函數將引發異常。

注意:無效的參數會導致未定義的行為。

例子1

讓我們看一個簡單的例子來演示 partial_sort() 的使用:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  vector<int> v = {3, 1, 4, 2, 5};
  
    cout<<"Before sorting:";
    for_each(v.begin(), v.end(), [](int x) {
    cout << x << " ";
  });

    partial_sort(v.begin(), v.begin() + 2, v.end());
  
  cout<<"\nAfter sorting: ";
  for_each(v.begin(), v.end(), [](int x) {
    cout << x << " ";
  });
  
  return 0;
}

輸出:

Before sorting:3 1 4 2 5 
After sorting: 1 2 4 3 5

例子2

讓我們看另一個簡單的例子:

#include <iostream>     // std::cout
#include <algorithm>    // std::partial_sort
#include <vector>       // std::vector

using namespace std;

bool myfunction (int i,int j) { return (i<j); }

int main () {
  int myints[] = {9,8,7,6,5,4,3,2,1};
  vector<int> myvector (myints, myints+9);

  // using default comparison (operator <):
  partial_sort (myvector.begin(), myvector.begin()+5, myvector.end());

  // using function as comp
  partial_sort (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction);

  // print out content:
  cout << "myvector contains:";
  for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  return 0;
}

輸出:

myvector contains:1 2 3 4 5 9 8 7 6

例子3

讓我們看一個默認版本的簡單示例:

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>

using namespace std ;

int main()
{
    const int VECTOR_SIZE = 8 ;

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

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

    IntVector Numbers(VECTOR_SIZE) ;

    IntVectorIt start, end, it ;

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

    start = Numbers.begin() ;   // location of first
                                // element of Numbers

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

    cout << "Before calling partial_sort\n" << endl ;

    // print content of Numbers
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;

    // sort the smallest 4 elements in the sequence
    partial_sort(start, start+4, end) ;

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

    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;
   
   return 0; 
}

輸出:

Before calling partial_sort

Numbers { 4 10 70 30 10 69 96 7  }

After calling partial_sort

Numbers { 4 7 10 10 70 69 96 30  }

示例 4

讓我們看一個自定義(謂詞)版本的簡單示例:

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>

using namespace std ;

int main()
{
    const int VECTOR_SIZE = 8 ;

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

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

    IntVector Numbers(VECTOR_SIZE) ;

    IntVectorIt start, end, it ;

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

    start = Numbers.begin() ;   // location of first
                                // element of Numbers

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

    cout << "Before calling partial_sort\n" << endl ;

    // print content of Numbers
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;

    // sort the smallest 4 elements in the sequence
    partial_sort(start, start+4, end, less<int>()) ;

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

    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;
    
    return 0;
}

輸出:

Before calling partial_sort

Numbers { 4 10 70 30 10 69 96 7  }

After calling partial_sort

Numbers { 4 7 10 10 70 69 96 30  }





相關用法


注:本文由純淨天空篩選整理自 C++ Algorithm partial_sort()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。