如何在 C++ 中创建自动类型的模板 Class 对象?
How to Create Template Class object of auto type in C++?
我是一名学习者,我使用的逻辑可能不正确(因为我仍处于学习阶段)。
我使用模板在 C++ 中创建了一个简单的堆排序代码。在主函数中,从堆 class 中为每种数据类型调用每个函数都会创建相同行的副本。所以,我想使用指针对象为所有数据类型编写通用函数调用,然后该指针对象将引用该堆的相应对象 class。
这是我出错的主要功能代码部分。
Heap<auto> *obj;
Heap<int> intOb(n);
Heap<char> charOb(n);
Heap<float> floatOb(n);
if (dataType == 1) {
obj = &intOb;
} else if (dataType == 2) {
obj = &charOb;
} else if (dataType == 3) {
obj = &floatOb;
} else {
cout << "Wrong Data Type\n";
exit(101); }
obj->inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj->printArray();
obj->buildMaxHeap();
cout << "The Heapified Array is ";
obj->printArray();
obj->heapSort();
cout << "The Array After Heap Sort is ";
obj->printArray();
cout << "The Total no of comparisons is " << obj->counter << endl;
如果我只是在 if else 循环中调用所有这些函数,代码将会重复。为了避免它们,我创建了一个 auto 类型的指针对象。但是,模板中不允许使用 auto。如果我用 int 替换 auto,那么对 char 和 float 的引用会出错。
有什么方法可以调整对象的创建,这样我就不必在 if else 循环中一次又一次地重复这些行了吗?
如果我的问题得不到解决,我将不得不多次重复相同的行,就像给定的代码一样。
if (dataType == 1) {
Heap<int> obj(n);
obj.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj.printArray();
obj.buildMaxHeap();
cout << "The Heapified Array is ";
obj.printArray();
obj.heapSort();
cout << "The Array After Heap Sort is ";
obj.printArray();
cout << "The Total no of comparisons is " << obj.counter << endl;
} else if (dataType == 2) {
Heap<char> obj(n);
obj.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj.printArray();
obj.buildMaxHeap();
cout << "The Heapified Array is ";
obj.printArray();
obj.heapSort();
cout << "The Array After Heap Sort is ";
obj.printArray();
cout << "The Total no of comparisons is " << obj.counter << endl;
} else if (dataType == 3) {
Heap<float> obj(n);
obj.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj.printArray();
obj.buildMaxHeap();
cout << "The Heapified Array is ";
obj.printArray();
obj.heapSort();
cout << "The Array After Heap Sort is ";
obj.printArray();
cout << "The Total no of comparisons is " << obj.counter << endl;
} else { cout << "Wrong Data Type\n"; }
if (dataType == 1) {
Heap<int> obj(n);
obj.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj.printArray();
obj.buildMaxHeap();
cout << "The Heapified Array is ";
obj.printArray();
obj.heapSort();
cout << "The Array After Heap Sort is ";
obj.printArray();
cout << "The Total no of comparisons is " << obj.counter << endl;
} else if (dataType == 2) {
Heap<char> obj(n);
obj.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj.printArray();
obj.buildMaxHeap();
cout << "The Heapified Array is ";
obj.printArray();
obj.heapSort();
cout << "The Array After Heap Sort is ";
obj.printArray();
cout << "The Total no of comparisons is " << obj.counter << endl;
} else if (dataType == 3) {
Heap<float> obj(n);
obj.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj.printArray();
obj.buildMaxHeap();
cout << "The Heapified Array is ";
obj.printArray();
obj.heapSort();
cout << "The Array After Heap Sort is ";
obj.printArray();
cout << "The Total no of comparisons is " << obj.counter << endl;
} else { cout << "Wrong Data Type\n"; }
使用(模板)函数完成所有主要工作,并将Heap
模板实例传递(引用)给函数。
可能是这样的:
template<typename T>
void work_on_heap(Heap<T> const& heap)
{
heap.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
heap.printArray();
heap.buildMaxHeap();
cout << "The Heapified Array is ";
heap.printArray();
heap.heapSort();
cout << "The Array After Heap Sort is ";
heap.printArray();
cout << "The Total no of comparisons is " << heap.counter << endl;
}
然后像这样打电话:
if (dataType == 1) {
work_on_heap(Heap<int>(n));
} else if (dataType == 2) {
work_on_heap(Heap<char>(n));
} else if (dataType == 3) {
work_on_heap(Heap<float>(n));
} else {
cout << "Wrong Data Type\n";
exit(1);
}
我是一名学习者,我使用的逻辑可能不正确(因为我仍处于学习阶段)。 我使用模板在 C++ 中创建了一个简单的堆排序代码。在主函数中,从堆 class 中为每种数据类型调用每个函数都会创建相同行的副本。所以,我想使用指针对象为所有数据类型编写通用函数调用,然后该指针对象将引用该堆的相应对象 class。
这是我出错的主要功能代码部分。
Heap<auto> *obj;
Heap<int> intOb(n);
Heap<char> charOb(n);
Heap<float> floatOb(n);
if (dataType == 1) {
obj = &intOb;
} else if (dataType == 2) {
obj = &charOb;
} else if (dataType == 3) {
obj = &floatOb;
} else {
cout << "Wrong Data Type\n";
exit(101); }
obj->inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj->printArray();
obj->buildMaxHeap();
cout << "The Heapified Array is ";
obj->printArray();
obj->heapSort();
cout << "The Array After Heap Sort is ";
obj->printArray();
cout << "The Total no of comparisons is " << obj->counter << endl;
如果我只是在 if else 循环中调用所有这些函数,代码将会重复。为了避免它们,我创建了一个 auto 类型的指针对象。但是,模板中不允许使用 auto。如果我用 int 替换 auto,那么对 char 和 float 的引用会出错。
有什么方法可以调整对象的创建,这样我就不必在 if else 循环中一次又一次地重复这些行了吗?
如果我的问题得不到解决,我将不得不多次重复相同的行,就像给定的代码一样。
if (dataType == 1) {
Heap<int> obj(n);
obj.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj.printArray();
obj.buildMaxHeap();
cout << "The Heapified Array is ";
obj.printArray();
obj.heapSort();
cout << "The Array After Heap Sort is ";
obj.printArray();
cout << "The Total no of comparisons is " << obj.counter << endl;
} else if (dataType == 2) {
Heap<char> obj(n);
obj.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj.printArray();
obj.buildMaxHeap();
cout << "The Heapified Array is ";
obj.printArray();
obj.heapSort();
cout << "The Array After Heap Sort is ";
obj.printArray();
cout << "The Total no of comparisons is " << obj.counter << endl;
} else if (dataType == 3) {
Heap<float> obj(n);
obj.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj.printArray();
obj.buildMaxHeap();
cout << "The Heapified Array is ";
obj.printArray();
obj.heapSort();
cout << "The Array After Heap Sort is ";
obj.printArray();
cout << "The Total no of comparisons is " << obj.counter << endl;
} else { cout << "Wrong Data Type\n"; }
if (dataType == 1) {
Heap<int> obj(n);
obj.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj.printArray();
obj.buildMaxHeap();
cout << "The Heapified Array is ";
obj.printArray();
obj.heapSort();
cout << "The Array After Heap Sort is ";
obj.printArray();
cout << "The Total no of comparisons is " << obj.counter << endl;
} else if (dataType == 2) {
Heap<char> obj(n);
obj.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj.printArray();
obj.buildMaxHeap();
cout << "The Heapified Array is ";
obj.printArray();
obj.heapSort();
cout << "The Array After Heap Sort is ";
obj.printArray();
cout << "The Total no of comparisons is " << obj.counter << endl;
} else if (dataType == 3) {
Heap<float> obj(n);
obj.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
obj.printArray();
obj.buildMaxHeap();
cout << "The Heapified Array is ";
obj.printArray();
obj.heapSort();
cout << "The Array After Heap Sort is ";
obj.printArray();
cout << "The Total no of comparisons is " << obj.counter << endl;
} else { cout << "Wrong Data Type\n"; }
使用(模板)函数完成所有主要工作,并将Heap
模板实例传递(引用)给函数。
可能是这样的:
template<typename T>
void work_on_heap(Heap<T> const& heap)
{
heap.inputArray();
cout << "---------------------------------------------------------\n";
cout << "The Array you have entered is ";
heap.printArray();
heap.buildMaxHeap();
cout << "The Heapified Array is ";
heap.printArray();
heap.heapSort();
cout << "The Array After Heap Sort is ";
heap.printArray();
cout << "The Total no of comparisons is " << heap.counter << endl;
}
然后像这样打电话:
if (dataType == 1) {
work_on_heap(Heap<int>(n));
} else if (dataType == 2) {
work_on_heap(Heap<char>(n));
} else if (dataType == 3) {
work_on_heap(Heap<float>(n));
} else {
cout << "Wrong Data Type\n";
exit(1);
}