堆排序——C++关于堆排序的库函数排序
2016-11-25 20:23
260 查看
C++中对于堆排序算法,其实是有一个专门的库函数:sort_heap
默认make_heap为按升序排序构造
make_heap(v.begin,v.end,greater<int>());//用定义好的函数greater来排降序
sort_heap(v.begin,v.end,greater<int>());
void sort_heap (RandomAccessIterator first, RandomAccessIterator last,Compare comp);
默认make_heap为按升序排序构造
make_heap(v.begin,v.end,greater<int>());//用定义好的函数greater来排降序
sort_heap(v.begin,v.end,greater<int>());
#include <iostream> // std::cout #include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap #include <vector> // std::vector #include <time.h> #include <stdlib.h> using namespace std; int main () { srand((unsigned)time(NULL)); vector<int> v;//将myints复制到v for(int i=0;i<10;i++)//随机生成10个数并排序 v.push_back(rand()%20); make_heap (v.begin(),v.end()); sort_heap (v.begin(),v.end()); cout << "final sorted range :"; for (unsigned i=0; i<v.size(); i++) cout << ' ' << v[i]; return 0; }
相关文章推荐
- C++中关于set的自定义排序函数的书写
- 关于c++中排序函数的应用
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- 关于C++的一些细节[五:函数-中级]
- 在c++中qsort()排序函数的使用qsort函数应用大全
- 一劳永逸:关于C/C++中指针、数组与函数复合定义形式的直观解释
- [C++]关于选择排序和二分法的举例
- 关于rownum排序分页会比分析函数排序快的问题。。。
- 关于rand()的函数: 请问一个关于C++的问题...
- 关于C++的函数模版
- C++笔试题 关于函数析构函数未调用的问题
- C++里关于类的函数问题~
- [原创] 一劳永逸:关于C/C++中指针、数组与函数复合定义形式的直观解释
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- 关于C++中函数指针的使用
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- 关于C++的一些细节[五:函数-基础]
- C++中关于文件操作的函数
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- 关于c++ 函数指针的问题