C++加强堆_c++ 加强堆,2024年最新高级物联网嵌入式开发面试题2024

50 阅读3分钟
//获取obj在vector的位置
//将obj改为target
int index = indexMap[obj];
heap[index] = target;
//修改obj的位置,value不变,但是key要改变
//采用的方式是删除后重新插入
indexMap.erase(obj);
indexMap[target] = index;
//向上或向下调整
AdjustUp(index);
AdjustDwon(index);

} //删除指定的值 void erase(const T& obj) { //获取尾部的值 T& replace = heap[heapSize - 1]; //获取obj在vector的位置 int index = indexMap[obj]; indexMap.erase(obj);

--heapSize;
//用尾部的值替换掉obj,然后调整
if (obj != replace) 
{
	heap[index] = replace;
	indexMap[replace] = index;
	resign(replace);
}
heap.pop\_back();

}

//调整obj所在的位置 void resign(const T& obj) { //obj位置的值发生改变,向上和向下调整 //向上调整和向下调整只会发生一个 AdjustUp(indexMap[obj]); AdjustDwon(indexMap[obj]); }


由于可以获取任意值在vector的位置,因此实现起来也很简单。删除值的操作和pop类似,只需要尾部的值替换掉然后调整即可。


而修改值只需要在修改后调整位置以及改变indexMap的映射即可。




---



## 附录


* 实现代码:



#include #include #include #include #include<unordered_map> #include<assert.h> using namespace std;

template<class T, class Compare> class HeapGreater { public: HeapGreater() {} //迭代器初始化 template HeapGreater(InputIterator first, InputIterator last) { while (first != last) { push(*first); first++; } } ~HeapGreater() {}

void push(const T& x)
{
	heap.push\_back(x);
	indexMap[x] = heapSize;
	AdjustUp(heapSize++);
}
void pop()
{
	assert(!empty());
	T& node = heap[0];
	//交换首尾元素,然后删除
	swap(0, heapSize - 1);
	heapSize--;
	indexMap.erase(node);
	heap.pop\_back();
	AdjustDwon(0);
}

//将obj改为target
void set(const T& obj,const T& target)
{
	//获取obj在vector的位置
	//将obj改为target
	int index = indexMap[obj];
	heap[index] = target;
	//修改obj的位置,value不变,但是key要改变
	//采用的方式是删除后重新插入
	indexMap.erase(obj);
	indexMap[target] = index;
	//向上或向下调整
	AdjustUp(index);
	AdjustDwon(index);
}
//删除指定的值
void erase(const T& obj) 
{
	//获取尾部的值
	T& replace = heap[heapSize - 1];
	//获取obj在vector的位置
	int index = indexMap[obj];
	indexMap.erase(obj);

	--heapSize;
	//用尾部的值替换掉obj,然后调整
	if (obj != replace) 
	{
		heap[index] = replace;
		indexMap[replace] = index;
		resign(replace);
	}
	heap.pop\_back();
}
T top()
{
	assert(!empty());
	return heap[0];
}
size_t size()
{
	return heapSize;
}
bool empty()
{
	return heap.empty();
}

private: //需要封装交换函数,因为heap和indexMap的值都需要交换 void swap(int i, int j) { T o1 = heap[i]; T o2 = heap[j]; heap[i] = o2; heap[j] = o1; indexMap[o2] = i; indexMap[o1] = j;

}
//向上调整算法,每push一个数都要调用向上调整算法,保证插入后是一个大堆
void AdjustUp(int child)
{
	Compare com;
	int parent = (child - 1) / 2;
	while (child > 0)
	{
		if (com(heap[parent], heap[child]))
		{
			swap(parent, child);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}
//向下调整算法,每次调用pop都要进行向下调整算法重新构成大堆
void AdjustDwon(int parent)
{
	Compare com;
	int child = parent \* 2 + 1;
	while (child < heapSize)
	{
		if (child + 1 < heapSize && com(heap[child], heap[child + 1]))
		{
			child++;
		}
		if (com(heap[parent], heap[child]))
		{
			swap(parent, child);
			parent = child;
			child = parent \* 2 + 1;
		}
		else
		{
			break;
		}
	}
}
//调整obj所在的位置
void resign(const T& obj)
{
	//obj位置的值发生改变,向上和向下调整
	//向上调整和向下调整只会发生一个
	AdjustUp(indexMap[obj]);
	AdjustDwon(indexMap[obj]);
}
vector<T> heap;
//反向索引表
unordered_map<T, int>indexMap;
int heapSize = 0;

};


* 测试代码



#include"HeapGreater.h"

void test_HeapGreater1() { HeapGreater<string, less> heap; heap.push("abc"); heap.push("ccc"); heap.push("bbb"); heap.push("dd"); heap.push("eee"); heap.push("yyy"); heap.push("ppp");

heap.erase("eee");
heap.set("ccc", "new ccc");
while (!heap.empty())
{
	cout << heap.top() << endl;
	heap.pop();
}

} struct cmp { bool operator()(string*& l, string*& r) { return *l < *r; } }; void test_HeapGreater2() { string* s1 = new string("abc"); string* s2 = new string("ccc"); string* s3 = new string("bbb"); string* s4 = new string("dd"); string* s5 = new string("eee"); string* s6 = new string("yyy"); string* s7 = new string("ppp"); string* s8 = new string("new ccc");

HeapGreater<string\*, cmp> heap;
heap.push(s1);
heap.push(s2);
heap.push(s3);
heap.push(s4);
heap.push(s5);
heap.push(s6);
heap.push(s7);

heap.erase(s5);
heap.set(s2, s8);
while (!heap.empty())
{
	cout << \*heap.top() << endl;
	heap.pop();
}

} void test_HeapGreater3() { vectorv = { "abc" ,"ccc" ,"bbb" ,"dd" ,"eee" ,"yyy" ,"ppp" }; HeapGreater<string, less> heap(v.begin(),v.end());

heap.erase("eee");
heap.set("ccc", "new ccc");
while (!heap.empty())
{
	
	cout << heap.top() << endl;
	heap.pop();
}

} int main() { test_HeapGreater1(); cout << endl << endl; test_HeapGreater2(); cout << endl << endl; test_HeapGreater3(); return 0; }


![在这里插入图片描述](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/9c46087040f847a4a888417fcd10171c~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771257777&x-signature=wcuIDY%2FV3pwqqqklRrwMkclfqyk%3D)








**收集整理了一份《2024年最新物联网嵌入式全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升的朋友。**
![img](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/0bbe0c85bb4f4473b81b48547ba51f89~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771257777&x-signature=read5BZRIvuk9j24B5Y%2FJmJNAdI%3D)
![img](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/2c0ab8fec46e4c969135fe9a20f361e5~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771257777&x-signature=9vYiRRAFNKy2ksk%2FVQegIJLUwqE%3D)

**[如果你需要这些资料,可以戳这里获取](https://gitee.com/vip204888)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人**

**都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**