尝试编译代码会产生一些错误(底部的错误代码)
//heap.h
#include <iostream>
#include <vector>
using namespace std;
template<class TYPE>
class Heap{
private:
vector<TYPE> heap;
int size;// number of elements in the heap
bool maxheap = true;
TYPE bubble_up(TYPE item);
TYPE bubble_down(TYPE item);
public:
Heap();
Heap(bool maxheap);
Heap(vector<TYPE>, bool order);
~Heap();
void build_heap();
TYPE Insert(TYPE item);
TYPE Delete(TYPE& item);
const vector<TYPE> sort(bool order);
const vector<TYPE> sort();// defualt sort if no variable given, max sort
TYPE get_size();
void print_heap();
void clear_heap();
};
template<class TYPE>
Heap<TYPE>::Heap(){
TYPE dummy{};
heap.push_back(dummy);
size = heap.size() - 1;
}
template<class TYPE>
Heap<TYPE>::Heap(bool order){
maxheap = order; // true is max, false is min
TYPE dummy{};
heap.push_back(dummy);
size = heap.size() - 1;
}
template<class TYPE>
Heap<TYPE>::Heap(vector<TYPE> x, bool order){
maxheap = order;// true is max, false is min
TYPE tempSize;
TYPE dummy{};
heap.push_back(dummy);
size = heap.size() - 1;
tempSize = x.size();
for (TYPE y = 0; y < tempSize; y++){
heap.push_back(x[y]);
}
size = heap.size() - 1;
build_heap();
}
template<class TYPE>
TYPE Heap<TYPE>::Insert(TYPE item){
heap.push_back(item);
size = heap.size() - 1;
return bubble_up(size);
}
TYPE Heap<TYPE>::bubble_up(TYPE pos){
TYPE retVal;
if (pos == 1)// root of tree
{
return pos;
}
if (maxheap == true){
if (heap[pos] > heap[pos / 2]){// greater than parent
TYPE temp = heap[pos / 2]; //swap method
heap[pos / 2] = heap[pos];
heap[pos] = temp;
return retVal = bubble_up(pos / 2);
}
else{
return pos;
}
}
if (maxheap == false){//min heap
if (heap[pos] < heap[pos / 2]){// less than parent
TYPE temp = heap[pos / 2]; //swap method
heap[pos / 2] = heap[pos];
heap[pos] = temp;
return retVal = bubble_up(pos / 2);
}
else{
return pos;
}
}
}
这是当前正在使用的驱动程序文件。
#include <iostream>
#include <string>
#include "Heap.h"
using std::cout;
using std::endl;
typedef string TYPE;
int main(void) {
Heap<std::string> *s_heap = new Heap<std::string>(); // string heap
std::string s_item = "0";
vector<std::string> s_blah(11, s_item);
cout << "\n*** Test insert elements ***";
cout << endl << s_heap->Insert("15");
cout << endl << s_heap->Insert("1");
cout << endl << s_heap->Insert("3");
cout << endl << s_heap->Insert("4");
cout << endl;
}
完整错误代码:
c:\users\\documents\visual studio 2013\projects\pa 3 templates\pa 3 templates\heap.h(85): error
C2664: 'std::string Heap<std::string>::bubble_up(TYPE)' : cannot convert argument 1 from 'int' to 'std::string'
1> with
1> [
1> TYPE=std::string
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1> c:\users\\documents\visual studio 2013\projects\pa 3 templates\pa 3 templates\heap.h(82) : while compiling class template member function 'std::string Heap<std::string>::Insert(TYPE)'
1> with
1> [
1> TYPE=std::string
1> ]
1> c:\users\\documents\visual studio 2013\projects\pa 3 templates\pa 3 templates\driver.cpp(17) : see reference to function template instantiation 'std::string Heap<std::string>::Insert(TYPE)' being compiled
1> with
1> [
1> TYPE=std::string
1> ]
答案 0 :(得分:0)
你在这里遇到了一些问题,主要与你试图制作Heap
通用的问题有关,而你没有做到这一点。
您正在尝试创建std::string
的堆(尽管您添加的所有值都是数字,但我们暂时跳过该形式)。
bubble_up
的参数也因此也是一个字符串:
TYPE Heap<TYPE>::bubble_up(TYPE pos){
问题出现在这样的行中:
if (pos == 1)// root of tree
和此:
heap[pos / 2] = heap[pos];
你试图在第一种情况下将字符串与数字进行比较,并尝试在第二种情况下对字符串执行除法!这不会起作用。如果你试图传入结构和类以及其他任何东西,问题只会变得更糟。
您需要强制执行TYPE是一个可以执行这些操作的整数类型,或者认真重写堆的工作方式。
要提供一个泛型堆,当提供非整数TYPE
时失败且出现明显错误,您可以这样做:
template<class TYPE>
class Heap{
private:
static_assert(std::is_integral<TYPE>::value, "Must be an integral type");
如果你真的希望你的堆可以使用字符串......那么。这完全是另一个问题。您可以尝试存储所有值的哈希值:
#include <functional>
// ...
std::hash<TYPE> hashfunc;
size_t hashval = hashfunc(pos);
为所有标准库类型定义了散列函数,并返回一个很好的整数size_t
值,可以将其提供给堆算法的其余部分,而不需要进行任何实质性的更改。如果你想从bubble_up
等返回有用的值,你仍然需要保持从哈希值到原始数据的映射,但我会把它作为练习留给读者。