std::partial_sort_copy
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <algorithm> 中定义
|
||
template< class InputIt, class RandomIt > RandomIt partial_sort_copy( InputIt first, InputIt last, |
(1) | |
template< class InputIt, class RandomIt, class Compare > RandomIt partial_sort_copy( InputIt first, InputIt last, |
(2) | |
排序部分中的元素按升序排列的范围
[first, last)
。在最d_first - d_last元素被移动到的范围内[d_first, d_first + n)
并进行排序。 n
的元素数进行排序(n = min(last - first, d_last - d_first))。要保留相等元素的顺序是不能保证。 operator<的第一个版本使用比较的元素,第二个版本使用给定的比较函数comp
.原文:
Sorts some of the elements in the range
[first, last)
in ascending order. At most d_first - d_last of the elements are moved to the range [d_first, d_first + n)
and then sorted. n
is the number of elements to sort (n = min(last - first, d_last - d_first)). The order of equal elements is not guaranteed to be preserved. The first version uses operator< to compare the elements, the second version uses the given comparison function comp
.目录 |
[编辑] 参数
first, last | - | 范围内的元素进行排序
|
d_first, d_last | - | 随机访问迭代器定义的目标范围内
原文: random access iterators defining the destination range |
comp | - | comparison function which returns true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function must not modify the objects passed to it. |
类型要求 | ||
-InputIt 必须满足 InputIterator 的要求。
| ||
-RandomIt 必须满足 ValueSwappable 和 RandomAccessIterator 的要求。
| ||
-The type of dereferenced RandomIt must meet the requirements of MoveAssignable and MoveConstructible .
|
[编辑] 返回值
一个迭代器的元素定义的排序范围的上边界,即d_first + min(last - first, d_last - d_first).
原文:
an iterator to the element defining the upper boundary of the sorted range, i.e. d_first + min(last - first, d_last - d_first).
[编辑] 复杂度
O(N·log(min(D,N)),N = std::distance(first, last),D = std::distance(d_first, d_last)应用
cmp
.原文:
O(N·log(min(D,N)), where N = std::distance(first, last), D = std::distance(d_first, d_last) applications of
cmp
.[编辑] 示例
下面的代码排序向量的整数,并将它们复制到一个较小的和较大的载体.
原文:
The following code sorts an vector of integers and copies them into a smaller and a larger vector.
#include <algorithm> #include <vector> #include <functional> #include <iostream> int main() { std::vector<int> v0{4, 2, 5, 1, 3}; std::vector<int> v1{10, 11, 12}; std::vector<int> v2{10, 11, 12, 13, 14, 15, 16}; std::vector<int>::iterator it; it = std::partial_sort_copy(v0.begin(), v0.end(), v1.begin(), v1.end()); std::cout << "Writing to the smaller vector in ascending order gives: "; for (int a : v1) { std::cout << a << " "; } std::cout << '\n'; if(it == v1.end()) std::cout << "The return value is the end iterator\n"; it = std::partial_sort_copy(v0.begin(), v0.end(), v2.begin(), v2.end(), std::greater<int>()); std::cout << "Writing to the larger vector in descending order gives: "; for (int a : v2) { std::cout << a << " "; } std::cout << '\n' << "The return value is the iterator to " << *it << '\n'; }
输出:
Writing to the smaller vector in ascending order gives: 1 2 3 The return value is the end iterator Writing to the larger vector in descending order gives: 5 4 3 2 1 15 16 The return value is the iterator to 15
[编辑] 另请参阅
将区间内较小的N个元素排序 (函数模板) | |
将区间按升序排序 (函数模板) | |
将区间内的元素排序,同时保持相等的元素之间的顺序 (函数模板) |