C++ 算法 中的 partial_sort函数

首页 / C++入门教程 / C++ 算法 中的 partial_sort函数

C++算法 partial_sort()函数用于重新排列range [first,last)中的元素,以这样的方式对第一个和中间的元素进行排序,而在中间和中间的元素进行排序最后将以不确定的顺序。

partial_sort - 语法

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);

partial_sort - 参数

first:一个随机访问迭代器,指向要部分排序的参数中的第一个元素。

last:一个随机访问迭代器,指向要部分排序的参数中的最后一个元素。

middle:一个随机访问迭代器,指向要排序的子参数中最后一个元素之后的那个。

comp :用户定义的二进制谓词函数,该函数接受两个参数,如果两个参数顺序正确,则返回true,否则返回false。它遵循严格的弱排序来对元素进行排序。

partial_sort - 返回值

没有

partial_sort - 例子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

partial_sort - 例子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

partial_sort - 例子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  }

partial_sort - 例子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  }

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Go语言核心36讲 -〔郝林〕

软件工程之美 -〔宝玉〕

Spring编程常见错误50例 -〔傅健〕

网络排查案例课 -〔杨胜辉〕

高并发系统实战课 -〔徐长龙〕

大型Android系统重构实战 -〔黄俊彬〕

Python实战 · 从0到1搭建直播视频平台 -〔Barry〕

结构思考力 · 透过结构看表达 -〔李忠秋〕

LangChain 实战课 -〔黄佳〕

好记忆不如烂笔头。留下您的足迹吧 :)