C++ STL Heap算法
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> vec1;
vector<int>::iterator vec_iter1;
for (int k=0;k<10;k++)
{
vec1.push_back(rand());
}
for (vec_iter1 = vec1.begin();vec_iter1 != vec1.end();++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "-------------------------------------------------------" << endl;
make_heap(vec1.begin(), vec1.end());
for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "-------------------------------------------------------" << endl;
//取出堆中最大的数放到最后面,剩下的重新排
pop_heap(vec1.begin(), vec1.end());
for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "-------------------------------------------------------" << endl;
//删除最后一个
vec1.pop_back();
for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "-------------------------------------------------------" << endl;
vec1.push_back(100);
for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "-------------------------------------------------------" << endl;
push_heap(vec1.begin(),vec1.end());
for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "-------------------------------------------------------" << endl;
sort_heap(vec1.begin(), vec1.end());
for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
{
cout << *vec_iter1 << " ";
}
cout << endl;
cout << "-------------------------------------------------------" << endl;
system("pause");
return 0;
}
=============================================================
41 18467 6334 26500 19169 15724 11478 29358 26962 24464
-------------------------------------------------------
29358 26962 15724 26500 24464 6334 11478 41 18467 19169
-------------------------------------------------------
26962 26500 15724 19169 24464 6334 11478 41 18467 29358
-------------------------------------------------------
26962 26500 15724 19169 24464 6334 11478 41 18467
-------------------------------------------------------
26962 26500 15724 19169 24464 6334 11478 41 18467 100
-------------------------------------------------------
26962 26500 15724 19169 24464 6334 11478 41 18467 100
-------------------------------------------------------
41 100 6334 11478 15724 18467 19169 24464 26500 26962
-------------------------------------------------------
请按任意键继续. . .
C++ STL Heap算法的更多相关文章
- STL——heap结构及算法
heap(隐式表述,implicit representation) 1. heap概述 : vector + heap算法 heap并不归属于STL容器组件,它是个幕后英雄,扮演priority q ...
- STL -- heap结构及算法
STL -- heap结构及算法 heap(隐式表述,implicit representation) 1. heap概述 : vector + heap算法 heap并不归属于STL容器组件,它是个 ...
- STL heap部分源代码分析
本文假设你已对堆排序的算法有主要的了解. 要分析stl中heap的源代码的独到之处.最好的办法就是拿普通的代码进行比較.话不多说,先看一段普通的堆排序的代码: //调整大顶堆.使得结构合理 void ...
- 常用的STL查找算法
常用的STL查找算法 <effective STL>中有句忠告,尽量用算法替代手写循环:查找少不了循环遍历,在这里总结下常用的STL查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...
- 【STL】帮你复习STL泛型算法 一
STL泛型算法 #include <iostream> #include <vector> #include <algorithm> #include <it ...
- STL基础--算法(排序)
STL排序算法 排序算法要求随机访问迭代器 vector, deque, container array, native array 例子 vector<int> vec = {9,1,1 ...
- C++复习:STL之算法
算法 1算法基础 1.1算法概述 算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. <algorithm> ...
- STL所有算法简介 (转) http://www.cnblogs.com/yuehui/archive/2012/06/19/2554300.html
STL所有算法简介 STL中的所有算法(70个) 参考自:http://www.cppblog.com/mzty/archive/2007/03/14/19819.htmlhttp://hi.baid ...
- 初探STL之算法
算法 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包括头文件<algor ...
随机推荐
- 2.Storm集群部署及单词统计案例
1.集群部署的基本流程 2.集群部署的基础环境准备 3.Storm集群部署 4.Storm集群的进程及日志熟悉 5.Storm集群的常用操作命令 6.Storm源码下载及目录熟悉 7.Storm 单词 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- [转]10分钟了解分布式CAP、BASE理论
原文: https://www.cnblogs.com/chengtian/p/11278072.html ---------------------------------------------- ...
- android studio调试报错:java.lang.RuntimeException: Unable to start activity ComponentInfo
报错信息: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pro_u_loc/com.e ...
- MySQL中的连接、实例、会话、数据库、线程之间的关系
MySQL中的实例.数据库关系简介 1.MySQL是单进程多线程(而Oracle等是多进程),也就是说MySQL实例在系 统上表现就是一个服务进程,即进程(通过多种方法可以创建多实例,再安装一个端口号 ...
- contos7上安装rabbitmq
#centeros7 安装erlang yum install erlang #启动扩展源 yum install epel-release #下载rabbitmq源文件 wget http://ww ...
- jQuery.map(arr|obj,callback)
jQuery.map(arr|obj,callback) 概述 将一个数组中的元素转换到另一个数组中.广州大理石机械构件 作为参数的转换函数会为每个数组元素调用,而且会给这个转换函数传递一个表示被转换 ...
- npm源管理
1. 安装淘宝镜像 为了提高npm的安装速度,可以使用淘宝镜像. 使用淘宝镜像的方法有两种: 1. npm install -g cnpm --registry=https://registry.np ...
- 安装Discuz
1.下载Discuz 版本文件 http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_GBK.zip 2.下载PHP http://window ...
- @MapperScan和@ComponentScan的区别
区别 今天在撸SpringBoot的时候,突然对注解产生了混淆,@MapperScan和@ComponentScan都是扫描包,二者之间有什么区别呢? 首先,@ComponentScan是组件扫描注解 ...