C++ 算法 中的 partial_sort_copy函数

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

C++算法 partial_sort_copy()函数类似于partial_sort()函数,该函数用于重新排列range [first,last)中的元素,这样第一和中间之间的元素将是排序后,中间和最后一个元素之间的顺序将不确定。但是partial_sort_copy()函数将结果放入新的参数[result_first,result_last)。

partial_sort_copy - 语法

default (1)      template <class InputIterator, class RandomAccessIterator>
                         RandomAccessIterator
                           partial_sort_copy (InputIterator first,InputIterator last,
                            RandomAccessIterator result_first,
                             RandomAccessIterator result_last);

custom (2)      template <class InputIterator, class RandomAccessIterator, class Compare>
  		RandomAccessIterator
   		 partial_sort_copy (InputIterator first,InputIterator last,
                            RandomAccessIterator result_first,
                              RandomAccessIterator result_last, Compare comp);

partial_sort_copy - 参数

first:一个输入迭代器,它指向源参数中要部分排序的第一个元素。

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

result_first :指向已排序目标参数中第一个元素的随机访问迭代器。

result_last :指向已排序目标参数中过去的最后一个元素的随机访问迭代器。

comp :用户定义的二进制谓词函数,该函数接受两个参数,如果两个参数顺序正确,则返回true,否则返回false。

partial_sort_copy - 返回值

它返回一个迭代器寻址,该迭代器寻址结果序列中最后写入的元素之后的元素。

partial_sort_copy - 例子1

让我们看一个简单的示例,以演示partial_sort_copy()的用法:

#include <iostream>    //std::cout
#include <algorithm>   //std::partial_sort_copy
#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 (5);

 //using default comparison (operator <):
  partial_sort_copy (myints, myints+9, myvector.begin(), myvector.end());

 //using function as comp
  partial_sort_copy (myints, myints+9, myvector.begin(), 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

partial_sort_copy - 例子2

让我们看一下默认版本的另一个简单示例:

#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) ;
    IntVector Result(4) ;

    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_copy\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 Numbers
   //and copy the results in Result
    partial_sort_copy(start, end, Result.begin(), Result.end()) ;

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

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

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

输出:

Before calling partial_sort_copy

Numbers { 4 10 70 30 10 69 96 7  }

After calling partial_sort_copy

Numbers { 4 10 70 30 10 69 96 7  }

Result { 4 7 10 10  }

partial_sort_copy - 例子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) ;
    IntVector Result(4) ;

    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_copy\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 Numbers
   //and copy the results in Result
    partial_sort_copy(start, end, Result.begin(),Result.end(),less<int>());

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

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

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

输出:

Before calling partial_sort_copy

Numbers { 4 10 70 30 10 69 96 7  }

After calling partial_sort_copy

Numbers { 4 10 70 30 10 69 96 7  }

Result { 4 7 10 10  }

partial_sort_copy - 例子4

让我们看另一个例子:

#include <algorithm>
#include <vector>
#include <functional>
#include <iostream>
using namespace std; 
 
vector<int>::iterator it;

void print(vector<int> & v)
{ 
    for (it = v.begin(); it != v.end(); it++) {
        cout << *it << ' ';
    }
    cout << '\n';
}
 
int main()
{
    vector<int> v0{4, 2, 5, 1, 3};
    vector<int> v1{10, 11, 12};
    vector<int> v2{10, 11, 12, 13, 14, 15, 16};
 
    cout << "v0 : ";
    print(v0);
 
    cout << "v1 : ";
    print(v1);
 
    cout << "v2 : ";
    print(v2);
 
    it = partial_sort_copy(v0.begin(), v0.end(), v1.begin(), v1.end());
 
    cout << "Writing v0 to v1 in ascending order gives: ";
    print(v1);
 
    it = partial_sort_copy(v0.begin(), v0.end(), v2.begin(), v2.end(), 
                                std::greater<int>());
 
    cout << "Writing v0 to v2 in descending order gives: ";
    print(v2);
    
    return 0;
} 

输出:

v0 : 4 2 5 1 3 
v1 : 10 11 12 
v2 : 10 11 12 13 14 15 16 
Writing v0 to v1 in ascending order gives: 1 2 3 
Writing v0 to v2 in descending order gives: 5 4 3 2 1 15 16

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

技术教程推荐

赵成的运维体系管理课 -〔赵成〕

数据结构与算法之美 -〔王争〕

小马哥讲Spring核心编程思想 -〔小马哥〕

架构实战案例解析 -〔王庆友〕

深入浅出云计算 -〔何恺铎〕

陶辉的网络协议集训班02期 -〔陶辉〕

技术面试官识人手册 -〔熊燚(四火)〕

云原生架构与GitOps实战 -〔王炜〕

结构学习力 -〔李忠秋〕

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