Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

partial_sort_copy

函数模板  <algorithm>
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);

对范围内的元素进行部分排序并复制

将[first,last)范围内的元素由小到大复制到[result_first,result_last)。 复制的元素数量与result_first和result_last之间的距离相同(除非这比[first,last)中的元素数量多)。

范围[first,last)没有被修改。

第一个版本使用operator<比较元素,第二个版本使用comp比较元素。

☲  参数


first, last
指向要复制的序列初始和最终位置的输入迭代器。使用的范围是[first,last), 它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。 输入迭代器(InputIterator)必须指向一个类型, 该类型可赋值给随机访问迭代器(RandomAccessIterator)所指向的元素。

result_first, result_last
指向目标序列的初始和最终位置的随机访问迭代器。使用的范围是[result_first,result_last]。 RandomAccessIterator应该指向一个正确定义了swap的类型,并且该类型既可以移动构造,也可以移动赋值。

comp
二元函数,它接受结果范围中的两个元素作为参数,并返回可转换为bool的值。 返回的值表明,作为第一个参数传递的元素是否按照其定义的严格弱顺序先于第二个参数。
函数不能修改它的任何参数。
它可以是函数指针,也可以是函数对象。

☉  返回值



指向结果序列中最后一个元素后面的元素的迭代器。

☣  示例



// partial_sort_copy example
#include <iostream>     // std::cout
#include <algorithm>    // std::partial_sort_copy
#include <vector>       // std::vector

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

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

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

  // using function as comp
  std::partial_sort_copy (myints, myints+9, myvector.begin(), myvector.end(), myfunction);

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

  return 0;
}

输出:
myvector contains: 1 2 3 4 5

✥ 复杂度



平均来说,在 first and last之间的距离小于线性: 对元素进行大约N*log(min(N,M))的比较(其中N是first and last之间的距离, M是result_first和result_last之间的距离)。 它还在范围之间执行最多数量的元素交换(或移动)和最小(N,M)赋值。

⇄ 数据竞争


范围[first,last)中的对象将被修改。

☂ 异常安全性



如果任何元素比较、元素交换(或移动)或迭代器上的操作抛出。,则抛出。
注意,无效的参数会导致未定义的行为。

🍄  另请参阅



partial_sort 对范围内的元素进行部分排序(函数模板)
sort 对范围内的元素进行排序(函数模板)
copy 复制范围的元素(函数模板)
remove_copy_if 复制范围内部分值(函数模板)

联系我们 免责声明 关于CandCplus 网站地图