STL algorithm算法is_heap和is_heap_until(25)
来源:互联网 发布:天界翅膀进化数据 编辑:程序博客网 时间:2024/05/16 23:48
is_heap原型:
std::is_heap
template <class RandomAccessIterator> bool is_heap (RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class Compare> bool is_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
该函数使用operator<进行比较(也就是说,第一个版本测试是否是一个大顶堆)。(堆的概念自己去找)
如果范围内的元素个数少于两个,总是返回true,因为总能组成大顶堆。
一个简单的测试例子:(可以自己画出堆的完全二叉树图看看)
#include <iostream>#include <vector>#include <array>#include <algorithm>using namespace std;void isheap(){ vector<int> vi{1,2,3,4,5,6,7}; vector<int> v2(vi.rbegin(),vi.rend()); cout<<"vi="; for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";}); cout<<endl; if(is_heap(vi.begin(),vi.end())) cout<<"is_heap(vi.begin(),vi.end()) return true!"<<endl; else cout<<"is_heap(vi.begin(),vi.end()) return false!"<<endl; cout<<"v2="; for_each(v2.begin(),v2.end(),[](int i){cout<<i<<" ";}); cout<<endl; if(is_heap(v2.begin(),v2.end())) cout<<"is_heap(v2.begin(),v2.end()) return true!"<<endl; else cout<<"is_heap(v2.begin(),v2.end()) return false!"<<endl;}运行截图:
———————————————————————————————————————————————————
is_heap_until原型:
std::is_heap_until
template <class RandomAccessIterator> RandomAccessIterator is_heap_until (RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class Compare> RandomAccessIterator is_heap_until (RandomAccessIterator first, RandomAccessIterator last Compare comp);
如果都有效,则返回last.
也就是说,返回第一个破坏二叉堆结构元素的迭代器。
例如一个序列:9 87 6 10 5 9
画成堆的结构如下:
可以看到,10是第一个破坏堆结构的元素,9也是,运行程序:
源代码:
#include <iostream>#include <vector>#include <array>#include <algorithm>using namespace std;void isheapuntil(){ vector<int> vi{9,8,7,6,10,5,9}; cout<<"vi="; for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";}); cout<<endl; auto it=is_heap_until(vi.begin(),vi.end()); cout<<"it="<<*it<<endl; array<int,7> ai{9,8,7,6,5,4,3}; cout<<"ai="; for_each(ai.begin(),ai.end(),[](int i){cout<<i<<" ";}); cout<<endl; auto it2=is_heap_until(ai.begin(),ai.end()); cout<<"it2="<<*it2<<endl;}运行截图:
可以看到第一次返回的结果是10.
第二个vector里面都是有效堆,返回的则是last.
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-16
于GDUT
——————————————————————————————————————————————————————————————————
0 0
- STL algorithm算法is_heap和is_heap_until(25)
- is_heap和is_heap_until 用法与源码剖析
- STL的is_heap算法
- STL algorithm算法is_sorted和is_sorted_until(28)
- STL algorithm算法lower_bound和upper_bound(31)
- STL algorithm算法make_heap和sort_heap(32)
- STL algorithm算法集合
- 【STL】algorithm算法介绍
- STL algorithm算法generate和generate_n(22)
- STL algorithm算法replace,replace_if和replace_copy,replace_copy_if(49)
- STL algorithm算法binary_search(5)
- STL algorithm算法copy(6)
- STL algorithm算法copy_backward(7)
- STL algorithm算法copy_if(8)
- STL algorithm算法copy_n(9)
- STL algorithm算法count(10)
- STL algorithm算法count_if(11)
- STL algorithm算法equal(12)
- 花生壳内网穿透、nat123映射p2p穿透,都是如何穿透的
- MySQL函数
- 一个简单的键盘钩子程序
- 抱怨IT公司人才缺乏?留住现有人才方是正途 ...
- HttpWatch学习帮助文档
- STL algorithm算法is_heap和is_heap_until(25)
- Solve password autocomplete by browser issue(浏览器密码自动填充解决方案)
- struts中最核心的类和包有哪些?
- 25亿美金!微软为自己的移动再次“补腿”
- WPF控件TextBlock中文字自动换行
- Matlab绘制Griewank函数三维图像
- Peersim 学习日记 02 初步认识Cycle-based
- Oracle收集统计信息详解
- 【创业】技术创业6年80W