C++11新特性应用--介绍几个新增的便利算法(stl中的heap使用,最大堆)
2016-01-21 22:31
465 查看
有的时候为了维护数据,我们使用stl的堆去维护一序列。
首先您要弄清楚堆和栈的区别,即heap和stack
stl中的堆默认是最大堆。
先介绍 push_heap,pop_heap,make_heap,sort_heap这四个算法,这四个不是C++11新增加的内容。
首先是如何产生一个最大推:
make_heap
原型:
第一个就是默认的,最大堆。
作用:
Rearranges the elements in the range [first,last) in such a way that they form a heap.
push_heap,pop_heap
这两个就不再赘述,在代码里有显现。
sort_heap
Sort elements of heap
看一段上面四个方法的应用:
接下来看看在上面的基础上,C++11又新增加了哪些内容:
is_heap
作用:
Returns true if the range [first,last) forms a heap, as if constructed with make_heap.
应用:
is_heap_until
作用:
Find first element not in heap order
Returns an iterator to the first element in the range [first,last) which is not in a valid position if the range is considered a heap (as if constructed with make_heap).
应用:
注:例子来源于www.cplusplus.com网站
首先您要弄清楚堆和栈的区别,即heap和stack
stl中的堆默认是最大堆。
先介绍 push_heap,pop_heap,make_heap,sort_heap这四个算法,这四个不是C++11新增加的内容。
首先是如何产生一个最大推:
make_heap
原型:
template <class RandomAccessIterator> void make_heap (RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare> void make_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp );
第一个就是默认的,最大堆。
作用:
Rearranges the elements in the range [first,last) in such a way that they form a heap.
push_heap,pop_heap
这两个就不再赘述,在代码里有显现。
sort_heap
Sort elements of heap
看一段上面四个方法的应用:
#include <iostream> // std::cout #include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap #include <vector> // std::vector int main() { int myints[] = { 10,20,30,5,15 }; std::vector<int> v(myints, myints + 5); std::make_heap(v.begin(), v.end()); std::cout << "initial max heap : " << v.front() << '\n'; std::pop_heap(v.begin(), v.end()); v.pop_back(); std::cout << "max heap after pop : " << v.front() << '\n'; v.push_back(99); std::push_heap(v.begin(), v.end()); std::cout << "max heap after push: " << v.front() << '\n'; std::sort_heap(v.begin(), v.end()); std::cout << "final sorted range :"; for (unsigned i = 0; i<v.size(); i++) std::cout << ' ' << v[i]; std::cout << '\n'; return 0; } //输出: //initial max heap : 30 //max heap after pop : 20 //max heap after push : 99 //final sorted range : 5 10 15 20 99
接下来看看在上面的基础上,C++11又新增加了哪些内容:
is_heap
作用:
Returns true if the range [first,last) forms a heap, as if constructed with make_heap.
应用:
#include <iostream> // std::cout #include <algorithm> // std::is_heap, std::make_heap, std::pop_heap #include <vector> // std::vector int main () { std::vector<int> foo {9,5,2,6,4,1,3,8,7}; if (!std::is_heap(foo.begin(),foo.end())) std::make_heap(foo.begin(),foo.end()); std::cout << "Popping out elements:"; while (!foo.empty()) { std::pop_heap(foo.begin(),foo.end()); // moves largest element to back std::cout << ' ' << foo.back(); // prints back foo.pop_back(); // pops element out of container } std::cout << '\n'; return 0; }
is_heap_until
作用:
Find first element not in heap order
Returns an iterator to the first element in the range [first,last) which is not in a valid position if the range is considered a heap (as if constructed with make_heap).
应用:
#include <iostream> // std::cout #include <algorithm> // std::is_heap_until, std::sort, std::reverse #include <vector> // std::vector int main () { std::vector<int> foo {2,6,9,3,8,4,5,1,7}; std::sort(foo.begin(),foo.end()); std::reverse(foo.begin(),foo.end()); auto last = std::is_heap_until (foo.begin(),foo.end()); std::cout << "The " << (last-foo.begin()) << " first elements are a valid heap:"; for (auto it=foo.begin(); it!=last; ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
注:例子来源于www.cplusplus.com网站
相关文章推荐
- C++11新特性应用--介绍几个新增的便利算法(stl中的heap使用,最大堆)
- C语言结构体
- C/C++ 学习日记第一棒(数据类型)
- 理解C语言——从小菜到大神的晋级之路(6)——函数与调用
- 改变矩阵元素的位置
- C++智能指针简单剖析
- C语言常用头文件总结
- 一起talk C栗子吧(第一百零八回:C语言实例--线程创建与结束一)
- const在C语言中作用
- c++ study diary2
- C语言判断当前机器大端小端
- c语言stack(栈)和heap(堆)的使用详解
- C语言:printf的执行顺序
- 第十七周oj刷题——Problem B: 分数类的四则运算【C++】
- [c++]类实现年收入计算
- Effective C++ 03:尽可能使用const
- C++ snprintf函数
- C/C++ 获取不带路径的文件名的方法
- 单链表的插入删除操作(c++实现)
- C语言:实现数据的四舍五入