类模板,允许用户定义类型C++
Class template, allow user to define type C++
我正在做一个关于使用向量的开放寻址哈希表的项目。该项目的一个方面是使用模板来允许用户定义矢量的类型。我已经实现了模板,如果我在编译之前创建一个对象,它可以工作,但是我不知道如何允许用户在运行时决定类型。
程序是面向对象的,类型必须传递到构造函数中。起初,我认为如果语句有效很简单,但是从那以后我了解到这不起作用。谁能帮我解决这个问题?谢谢
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template <class T>
class OpenHash
{
private:
vector <T> hashTab;
vector <int> emptyCheck;
int hashF(string);
public:
OpenHash(int);
int getVectorCap();
int addRecord (T);
int sizeHash();
int find(T);
int printHash();
int deleteEntry(T);
};
template <class T>
OpenHash<T>::OpenHash(int vecSize)
{
hashTab.clear();
hashTab.resize(vecSize);
emptyCheck.resize(vecSize);
for (int i=0; i < emptyCheck.capacity(); i++)
{
emptyCheck.at(i) = 0;
}
}
template <class T>
int OpenHash<T>::getVectorCap()
{
int cap = hashTab.capacity();
int capE = emptyCheck.capacity();
cout << cap << capE;
return 0;
}
template <class T>
int OpenHash<T>::hashF (string key)
{
int ascii = 0, hasVal = 0;
for (int i = 0; i < key.size(); i++)
{
ascii += key[i];
}
hasVal = ascii % hashTab.capacity();
return hasVal;
}
template <class T>
int OpenHash<T>::addRecord(T key)
{
if (sizeHash() == emptyCheck.size())
{
cout << "Your hash table is full cannot add any more records" << endl;
}
else
{
bool findPos = false;
for (int j=0; j<hashTab.capacity(); j++)
{
if (hashTab.at(j) == key)
{
cout << "Element ready exists in hashtable" << endl;
return 0;
}
}
int hashVal = hashF(key);
for (int i=hashVal; i<emptyCheck.capacity(); i++)
{
if (emptyCheck.at(i) == 0)
{
hashTab.at(i) = key;
emptyCheck.at(i) = 1;
cout << "Element added at" << '[' << i << ']' << endl;
findPos = true;
return 0;
}
else
{
cout << "Collision probing through..." << endl;
}
}
if (findPos == false)
{
for (int x=0; x<emptyCheck.capacity(); x++)
{
if (emptyCheck.at(x) == 0)
{
hashTab.at(x) = key;
emptyCheck.at(x) = 1;
cout << "Element added at" << '[' << x << ']' << endl;
findPos=true;
return 0;
}
}
cout << "Element could not be added" << endl;
}
}
return 1;
}
template <class T>
int OpenHash<T>::sizeHash()
{
int elementsFilled = 0;
for (int i = 0; i<emptyCheck.capacity(); i++)
{
if (emptyCheck.at(i) != 0)
{
elementsFilled++;
}
}
return elementsFilled;
}
template <class T>
int OpenHash<T>::find(T key)
{
int hashVal = hashF(key);
bool findLoop = false;
for (int i=hashVal; i < hashTab.capacity(); i++)
{
if (hashTab.at(i) == key && emptyCheck.at(i) == 1)
{
cout << "Element found at position: " << '[' << i << ']' << endl;
cout << key << endl;
findLoop = true;
return 0;
}
else
{
cout << "Element not found at position! Probing through..." << endl;
}
}
if (findLoop == false)
{
for (int j = 0; j < hashTab.capacity(); j++)
{
if (hashTab.at(j) == key && emptyCheck.at(j) != 0)
{
cout << "Element found at position: " << '[' << j << ']' << endl;
cout << key << endl;
findLoop = true;
return 0;
}
}
cout << "Entry not found" << endl;
}
return 1;
}
template <class T>
int OpenHash<T>::deleteEntry(T toDel)
{
int hashVal = hashF(toDel);
bool foundDel = false;
for (int i = hashVal; i<hashTab.capacity(); i++)
{
if (hashTab.at(i) == toDel)
{
emptyCheck.at(i) = 0;
foundDel = true;
cout << "Entry deleted!" << endl;
return 0;
}
}
if (foundDel == false)
{
for (int j=0; j<hashTab.capacity(); j++)
{
if (hashTab.at(j) == toDel)
{
emptyCheck.at(j) = 0;
foundDel = true;
cout << "Entry deleted!" << endl;
return 0;
}
}
cout << "The member to delete was not found" << endl;
}
return 1;
}
template <class T>
int OpenHash<T>::printHash()
{
if (sizeHash() == 0)
{
cout << "No elements filled to print!" << endl;
}
for (int i=0; i<emptyCheck.capacity(); i++)
{
if (emptyCheck.at(i) != 0)
{
cout << "Record at:" << '[' << i << ']' << hashTab.at(i) << endl;
}
}
return 0;
}
int main ()
{
cout << "Please input the size of your HashTable" << endl;
int vecSize = 0;
cin >> vecSize;
OpenHash<string> newTable(vecSize);
bool menu = true;
char choice;
while (menu == true)
{
cout << "Welcome to the open address hash table program, please input your choice:" << endl;
cin >> choice;
if (choice == 'a')
{
cout << "Please type in the record you wish to add:" << endl;
string rec;
getline(cin, rec);
newTable.addRecord(rec);
}
if (choice == 's')
{
cout << "Number of elements filled: " << newTable.sizeHash() << endl;
}
if (choice == 'f')
{
cout << "Please enter the string you wish to find" << endl;
string key;
getline(cin, key)
newTable.find(key);
}
if (choice == 'p')
{
cout << "Printing table..." << endl;
newTable.printHash();
}
if (choice == 'd')
{
cout << "Please input the item you wish to delete:" << endl;
string toDel;
getline(cin, toDel)
newTable.deleteEntry(toDel);
}
if (choice == 'x')
{
cout << "Thankyou" << endl;
menu = false;
}
}
return 0;
}
模板在编译时"决定"。 它们不能在运行时动态决定。
如果您想支持某些类型(例如 int
、double
、char
等),你可以在程序中显式声明它们,它们将被编译,但只有你支持的类型(或你在程序中使用的其他类型)可供用户"选择":
template<typename T>
class MyTemplateClass { ... };
class template MyTemplateClass<int>;
class template MyTemplateClass<double>;
class template MyTemplateClass<char>;
int main()
{
// if user wants to create a new int version:
MyTemplate<int> myInt;
// etc ...
return 0;
}
相关文章:
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- Visual C++(VS2017)中用户定义的转换不明确
- 使用用户定义的参数调用future/async并调用类方法
- 带有用户定义类的c++折叠表达式
- g++用户定义的动态链接库上的全局new和delete运算符
- 直接在 unordered_map 的方法中使用哈希,而不是生成哈希的用户定义对象
- 修改"std::set"中用户定义类型的值
- 参数包构造函数在类模板中隐藏用户定义的转换
- MAKE:找不到包含的用户定义的头文件?
- C++:用户定义的显式类型转换函数错误
- 从用户定义的类生成格式字符串?
- 为用户定义的类正确调用复制构造函数/赋值运算符
- C++ 向量与用户定义的类比较?(==, <, >)
- STL 用户定义的二进制操作
- 将用户定义的类型与 std::vector 和 std::sort 一起使用
- 为什么用户定义的函数不按照给定的顺序对相同长度的元素进行排序?
- 使用宏编译时使用用户定义的数学函数,或者仅使用 c++ 中标准数学库中的函数
- C++:用户定义的类,以成员字段作为地址