cb50a_c++_STL_算法_局部排序partial_sort

partial_sort(b,se,e)排序一部分,begin,source end,end
cout << "部分排序,开头的5个数排序" << endl;
partial_sort(ideq.begin(), ideq.begin() + 5, ideq.end());

需要注意的是,不能保持未排序元素的原始顺序。在执行 partial_sort() 后后面元素的顺序是不确定的,这取决于具体的实现。

partial_sort(b,se,e,p), begin,source end,end,paramete
partial_sort(ideq.begin(), ideq.begin() + 5, ideq.end(),greater<int>());//从大到小

partial_sort_copy(sb,se,db,de) 边排序边copy,sb(source begin),se(source end),db(destination begin)
partial_sort_copy(sb,se,db,de,p)

排序算法:
sort() 排序
stable_sort()稳定排序
https://www.cnblogs.com/txwtech/p/12366186.html

partial_sort()
partial_sort_copy(sb,se,db,de),
nth_element()

partition()分区
stable_partition()稳定分区
https://www.cnblogs.com/txwtech/p/12365880.html

make_heap()  构造一个大顶堆
push_heap()
pop_heap()
sort_heap()堆排序

greater<int>()是一个泛型

3,4,5,6,7,2,3,4,5,6,1,2,3,4,5

//

原理参考:
partial_sort 原理概述
那么 partial_sort 的原理是什么呢?是堆排序!

partial_sort的原理:

对原始容器内区间为[first, middle)的元素执行 make_heap() 操作构造一个大顶堆,
然后遍历剩余区间[middle, last)中的元素,剩余区间的每个元素均与大顶堆的堆顶元素进行比较(大顶堆的堆顶元素为最大元素,该元素为第一个元素,很容易获得),若堆顶元素较小,则交换堆顶元素和遍历得到的元素值(pop_heap ),并重新调整该大顶堆以维持该堆为大顶堆(adjust_heap)。
遍历结束后,[first, middle)区间内的元素便是排名在前的m个元素,再对该堆做一次堆排序 sort_heap() 便可得到最后的结果。

STL之partial_sort算法源码讲解
https://blog.csdn.net/ggq89/article/details/88817085

/*cb50a_c++_STL_算法_局部排序partial_sort

partial_sort(b,se,e)排序一部分,begin,source end,end
cout << "部分排序,开头的5个数排序" << endl;
partial_sort(ideq.begin(), ideq.begin() + 5, ideq.end()); 需要注意的是,不能保持未排序元素的原始顺序。在执行 partial_sort() 后后面元素的顺序是不确定的,这取决于具体的实现。 partial_sort(b,se,e,p), begin,source end,end,paramete
partial_sort(ideq.begin(), ideq.begin() + 5, ideq.end(),greater<int>());//从大到小 partial_sort_copy(sb,se,db,de) 边排序边copy,sb(source begin),se(source end),db(destination begin)
partial_sort_copy(sb,se,db,de,p) 排序算法:
sort() 排序
stable_sort()稳定排序
https://www.cnblogs.com/txwtech/p/12366186.html partial_sort()
partial_sort_copy(sb,se,db,de),
nth_element() partition()分区
stable_partition()稳定分区
https://www.cnblogs.com/txwtech/p/12365880.html make_heap()
push_heap()
pop_heap()
sort_heap() greater<int>()是一个泛型 3,4,5,6,7,2,3,4,5,6,1,2,3,4,5 // 原理参考:
partial_sort 原理概述
那么 partial_sort 的原理是什么呢?是堆排序! partial_sort的原理: 对原始容器内区间为[first, middle)的元素执行 make_heap() 操作构造一个大顶堆,
然后遍历剩余区间[middle, last)中的元素,剩余区间的每个元素均与大顶堆的堆顶元素进行比较(大顶堆的堆顶元素为最大元素,该元素为第一个元素,很容易获得),若堆顶元素较小,则交换堆顶元素和遍历得到的元素值(pop_heap ),并重新调整该大顶堆以维持该堆为大顶堆(adjust_heap)。
遍历结束后,[first, middle)区间内的元素便是排名在前的m个元素,再对该堆做一次堆排序 sort_heap() 便可得到最后的结果。 STL之partial_sort算法源码讲解
https://blog.csdn.net/ggq89/article/details/88817085
*/
#include <iostream>
#include <algorithm>
#include <deque>
#include <functional> using namespace std; template <typename TT9>
void print9(TT9 &ideq)
{
for (TT9::iterator iter = ideq.begin(); iter != ideq.end(); ++iter)
cout << *iter << ' ';
cout << endl; } int main()
{
deque<int> ideq;
for (int i = ; i <= ; ++i)
ideq.push_back(i);
for (int i = ; i <= ; ++i)
ideq.push_back(i);
for (int i = ; i <= ; ++i)
ideq.push_back(i);
print9(ideq); cout << "部分排序,开头的5个数排序" << endl;
partial_sort(ideq.begin(), ideq.begin() + , ideq.end());//默认从小到大,less<int>()
print9(ideq); partial_sort(ideq.begin(), ideq.begin() + , ideq.end(),greater<int>());//从大到小 print9(ideq); return ;
}
/*

*/

#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <functional>
#include <iterator> using namespace std; template <class TT99>
void print99(TT99 ideq)
{
for (TT99::iterator iter = ideq.begin(); iter != ideq.end(); ++iter)
cout << *iter << ' ';
cout << endl;
} int main()
{
deque<int> ideq;
vector<int> ivec6();
vector<int> ivec30(); for (int i = ; i <= ; ++i)
ideq.push_back(i);
for (int i = ; i <= ; ++i)
ideq.push_back(i);
for (int i = ; i <= ; ++i)
ideq.push_back(i);
print99(ideq); partial_sort_copy(ideq.begin(), ideq.end(), ivec6.begin(), ivec6.end()); //print99(ivec6);
cout << "copy到cout里面" << endl;
copy(ivec6.begin(), ivec6.end(), ostream_iterator<int>(cout, " "));
cout << endl; //用一个迭代器接受partial_sort_copy返回的结果,迭代器的位置
vector<int>::iterator pos;
pos=partial_sort_copy(ideq.begin(), ideq.end(), ivec30.begin(), ivec30.end());
cout << "直接调用自定义的函数,显示全部" << endl;
print99(ivec30);
cout << "只显示copy的部分," << endl; for (vector<int>::iterator iter = ivec30.begin(); iter != pos; ++iter)
cout << *iter << ' ';
cout << endl; return ;
}

cb50a_c++_STL_算法_局部排序partial_sort的更多相关文章

  1. STL_算法_局部排序(partial_sort、partial_sort_copy)

    C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) /***************************************** // partial_sort(b, ...

  2. cb51a_c++_STL_算法_根据第n个元素排序nth_element

    cb51a_c++_STL_算法_根据第n个元素排序nth_elementnth_element(b,n,e),比如最大的5个数排序,或者最小的几个数nth_element(b,n,e,p)对比:pa ...

  3. cb49a_c++_STL_算法_对所有元素排序_sort_stable_sort

    cb49a_c++_STL_算法_对所有元素排序_sort_stable_sort sort(b,e) sort(b,e,p) stable_sort(b,e) stable_sort(b,e,p) ...

  4. cb47a_c++_STL_算法_排列组合next_prev_permutation

    cb47a_c++_STL_算法_排列组合next_prev_permutation 使用前必须先排序.必须是 1,2,3或者3,2,1.否者结果不准确.如果, 1,2,4,6.这样数据不会准确nex ...

  5. cb36a_c++_STL_算法_区间的比较equal_mismatch_lexicographical_compare

    *cb36a_c++_STL_算法_区间的比较equal_mismatch_lexicographical_compare 区间:容器中的全部数据或者部分数据,都叫做区间 equal(b,e,b2), ...

  6. cb34a_c++_STL_算法_查找算法_(7)_lower_bound

    cb34a_c++_STL_算法_查找算法_(7)_lower_bound//针对已序区间的查找算法,如set,multiset关联容器-自动排序lower_bound()--第一个可能的位置uppe ...

  7. cb33a_c++_STL_算法_查找算法_(6)binary_search_includes

    cb33a_c++_STL_算法_查找算法_(6)binary_search_includes//针对已序区间的查找算法,如set,multiset关联容器-自动排序binary_search(b,e ...

  8. cb32a_c++_STL_算法_查找算法_(5)adjacent_find

    cb32a_c++_STL_算法_查找算法_(5)adjacent_findadjacent_find(b,e),b,begin(),e,end()adjacent_find(b,e,p),p-par ...

  9. cb28a_c++_STL_算法_查找算法_(1)find_find_if

    cb28a_c++_STL_算法_查找算法_(1)find_find_iffind() //线性查找,比较慢.pos1 = find(ilist.begin(), ilist.end(), 5);fi ...

随机推荐

  1. JS中的bind 、call 、apply

    # 一 .bind 特点: ### 1.返回原函数的拷贝,我们称这个拷贝的函数为绑定函数 ### 2.将函数中的this固定为调用bind方法时的第一个参数,所以称之为绑定函数.注意是名词而非动词. ...

  2. Thinkphp5 post提交模糊查询带分页如何保留参数

    最近做了一个分页的模糊查询post请求,发现查出来的分页点击下一页导致所有的搜索条件被重置,分页效果就失效了. 以下是网上部分解决办法: 控制器代码 public function index($na ...

  3. 循序渐进VUE+Element 前端应用开发(4)--- 获取后端数据及产品信息页面的处理

    在前面随笔<循序渐进VUE+Element 前端应用开发(3)--- 动态菜单和路由的关联处理>中介绍了在Vue + Element整合框架中,实现了动态菜单和动态路由的处理,从而可以根据 ...

  4. C/C++多参数函数参数的计算顺序与压栈顺序

    一.前言 今天在看Thinking in C++这本书时,书中的一个例子引起了我的注意,具体是使用了下面这句 单看这条语句的语义会发现仅仅是使用一个简单的string的substr函数将所得子串pus ...

  5. 50个SQL语句(MySQL版) 问题二

    --------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 tea ...

  6. Alpha冲刺 —— 5.4

    这个作业属于哪个课程 软件工程 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 Alpha冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.会议内容 1.展 ...

  7. Rocket - util - Misc

    https://mp.weixin.qq.com/s/kf4FvAFye_bRdT49Yow7Hg   简单介绍Misc中各个辅助方法的用途和实现.   ​​   1. ParameterizedBu ...

  8. Java实现 LeetCode 785 判断二分图(分析题)

    785. 判断二分图 给定一个无向图graph,当这个图为二分图时返回true. 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我 ...

  9. Java实现 LeetCode 705 设计哈希集合(使用数组保存有没有被用过)

    705. 设计哈希集合 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中 ...

  10. Java实现 LeetCode 664 奇怪的打印机(DFS)

    664. 奇怪的打印机 有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印同一个字符序列. 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符. 给定一个只包含小写英文字母的字符 ...