首页 > 编程语言 >c++针对特定数据结构创建堆

c++针对特定数据结构创建堆

时间:2023-01-29 21:37:08浏览次数:61  
标签:edgeWeights const 创建 value c++ heap 数据结构 ds CMP

make_heap

https://en.cppreference.com/w/cpp/algorithm/make_heap

struct ds 
{
	double value; 
	int idx;       
	ds(double v, int index) : value(v), idx(index){}
};
   
// 自定义比较函数   
class CMP {
public:
	bool operator()(const ds& a, const ds& b) {
		return a.value< b.value;
	}
};

std::vector<ds> edgeWeights;
std::make_heap(edgeWeights.begin(), edgeWeights.end(), CMP());  // max heap

while (edgeWeights.size() > 0) {
	std::pop_heap(edgeWeights.begin(), edgeWeights.end(), CMP());
	ds top = edgeWeights.back();
	edgeWeights.pop_back();
	continue; 
}

priority_queue

https://en.cppreference.com/w/cpp/container/priority_queue


struct ds
{
	double value;
	int idx;
	ds(double d, int index) : value(d), idx(index) {}
};

struct CMP : public std::binary_function<ds, ds, bool>
{	
	bool operator()(const ds& _Left, const ds& _Right) const
	{	
		return _Left.value > _Right.value;   // min heap
	}
};
typedef std::priority_queue<ds, std::vector<ds>, CMP > DistancePriorityQueue;

priQueue.push(ds(2.0, 0));

while (!priQueue.empty())
{
	ds di = priQueue.top();
	priQueue.pop();
}
			

标签:edgeWeights,const,创建,value,c++,heap,数据结构,ds,CMP
From: https://www.cnblogs.com/xiaxuexiaoab/p/17071682.html

相关文章

  • C/C++不知道为什么最后输出是1不是0
    提问: 我觉得应该输出9876543210不知道为什么会是9876543211  而这样写就没问题  解答: 第一个循环用的是前置--,走到n=1的时候,前置--为0,不会进while循环,不会......
  • C++ : 引发了异常: 写入访问权限冲突。 this 是 nullptr。
    在写代码的时候遇到了一个问题引发了异常:写入访问权限冲突。this是nullptr。程序抛异常。前情提要:MFC程序,我自己写了一个类MyVolt,里面有一个成员函数CollectVolt......
  • C++复健:运算符重载,实现string容器,实现string和vector的迭代器
    使得对象的运算像内置类型一样a.operator+(b);重载运算符的一些注意点:不能重载运算符操作基础数据类型:(1)重载运算符必须和用户定义的class类型一起使用(2)重载的运算符......
  • C++终章:探讨C++ 11 新标准
    一、前述此为《C++PrimerPlus(第6版)》一书的终章,本章对前面学习的一些C++11新性能做了提要总结,并针对移动语义、包装器、lambda表达式等新性能做了专门的拓展和介绍,本白......
  • [C++]Makefile概要
    ####Makefile变量和赋值符##延迟赋值: = 变量的正常设置,但值字段中提到的任何其他变量都在使用变量时用其值递归展开,而不是声明变量时的值## 延迟变量使用[......
  • 奇巧:C++ 调用python方法
    方法一:使用python提供给C/C++的API主流方法将python程序编程文本形式的动态链接库,在c/c++程序中调用其中定义的函数。本质上是在c++中启动了一个python解释器,由......
  • C++ Day10 统计圣经文本的词频 &文本查询程序
    一、编程题--统计圣经出现的单词以及词频统计一篇英文(The_Holy_Bible.txt)文章中出现的单词和词频,输入:某篇文章的绝对路径;输出:词典(词典中的内容为每一行都是一个“单词......
  • VC++的Unicode编程
    一、什么是Unicode先从ASCII说起,ASCII是用来表示英文字符的一种编码规范。每个ASCII字符占用1个字节,因此,ASCII编码可以表示的最大字符数是255(00H—FFH)。其实,英文字符......
  • gdb同时调试python和c++
    说明:当我们的python程序的一些函数的后端实现为C++时(比如Pytorch,TensorFlow或tvm等)可以使用当前方法调试。有两种方式可以安装调试环境:一.搭建环境有两种方式搭建......
  • Python和C++联合调试
     python和c++分别在Linux和Windows下联合调试首先创建一个python测试项目和一个c++拓展项目一、在Windows下进行调试1.编译器安装2.C拓展模块安装3.调试......