如何在不使用STL的情况下实现C++字典数据结构
How to implement C++ dictionary data structure without using STL
我正在做一个项目,主要目标是将单词列表(以及其中许多15k以上)加载到数据结构中,然后在该结构上进行搜索。我做了一点研究,据我所知,哈希表将是最好的(如果我错了,请纠正我,我也研究了尝试)
这是棘手的部分:我不能在这个项目中使用任何STL。因此,据我所知,我将不得不编写自己的哈希表类,或者找到一个非常有效的类。我知道表格是如何在基本水平上工作的,但我不确定我是否知道自己能写一整张表格。
我环顾谷歌,找不到任何合适的示例代码。
我的问题是,有人知道如何在c++中做到这一点吗?和/或在哪里我可以找到一些代码来开始。我需要表的3个基本功能:插入、搜索、删除。
当你思考这个时需要记住的事情:
- 首要问题是速度!这需要快速照明,而不考虑系统资源。根据我所做的阅读,哈希表可以比O(logn)做得更好考虑多线程
- 无法使用STL
我认为,排序字符串数组+二进制搜索应该非常高效。
std::unordered_map
不是STL
http://www.cs.auckland.ac.nz/software/AlgAnim/hash_tables.html
不完全清楚所有的限制,但假设你不能使用std中的任何东西,你可以写一个像下面这样的简单类来完成这项工作。我们将使用一个bucket数组来存储数据,然后使用哈希函数将字符串转换为0…MAX_ELEMENTS范围内的数字。每个bucket都将包含一个字符串链表,因此您可以再次检索信息。通常o(1)插入和查找。
请注意,对于更有效的解决方案,您可能希望使用向量,而不是像我所说的那样使用固定长度的数组。也有最小的错误检查和其他改进,但这应该让你开始。
注意,你需要实现自己的字符串哈希函数,你可以在网上找到很多这样的函数。
class dictionary
{
struct data
{
char* word = nullptr;
data* next = nullptr;
~data()
{
delete [] word;
}
};
public:
const unsigned int MAX_BUCKETS;
dictionary(unsigned int maxBuckets = 1024)
: MAX_BUCKETS(maxBuckets)
, words(new data*[MAX_BUCKETS])
{
memset(words, 0, sizeof(data*) * MAX_BUCKETS);
}
~dictionary()
{
for (int i = 0; i < MAX_BUCKETS; ++i)
delete words[i];
delete [] words;
}
void insert(const char* word)
{
const auto hash_index = hash(word);
auto& d = words[hash_index];
if (d == nullptr)
{
d = new data;
copy_string(d, word);
}
else
{
while (d->next != nullptr)
{
d = d->next;
}
d->next = new data;
copy_string(d->next, word);
}
}
void copy_string(data* d, const char* word)
{
const auto word_length = strlen(word)+1;
d->word = new char[word_length];
strcpy(d->word, word);
printf("%sn", d->word);
}
const char* find(const char* word) const
{
const auto hash_index = hash(word);
auto& d = words[hash_index];
if (d == nullptr)
{
return nullptr;
}
while (d != nullptr)
{
printf("checking %s with %sn", word, d->word);
if (strcmp(d->word, word) == 0)
return d->word;
d = d->next;
}
return nullptr;
}
private:
unsigned int hash(const char* word) const
{
// :TODO: write your own hash function here
const unsigned int num = 0; // :TODO:
return num % MAX_BUCKETS;
}
data** words;
};
http://wikipedia-clustering.speedblue.org/trie.php
以上链接目前似乎已断开。
备选链接:https://web.archive.org/web/20160426224744/http://wikipedia-clustering.speedblue.org/trie.php
源代码:https://web.archive.org/web/20160426224744/http://wikipedia-clustering.speedblue.org/download/libTrie-0.1.gz
- 在没有未定义行为的情况下实现类似std::vector的容器
- C++标准是否允许<double>在没有开销的情况下实现 std::可选
- 我可以在没有堆栈的情况下在二叉搜索树中实现迭代器吗?
- 如何在编译器C++不智能的情况下实现 GLSL vec* 构造语法?
- 为什么在这种情况下不调用我的虚拟函数实现?
- 我们可以在不使用head指针的情况下通过使用head的简单变量而不是head的指针来实现链表吗
- 在确定有实现的情况下获取HTTP 501未实现
- 在不创建新节点的情况下实现带有映射的trie
- 在这种情况下,有什么正确的方法可以实现锁定吗?
- 如何在不停滞主循环的情况下实现对话系统?
- 如何在不引入未来对象切片的情况下实现 ICloneable
- 是否可以在不依赖内置编译器的情况下拥有is_trivially_constructible的库实现
- 如何在没有参数的情况下实现返回双向链表大小的函数?int size() const.
- 有没有办法在不重新实现的情况下从子类中标记父级的虚拟函数 final。
- 如何在不使用函数或类的情况下重复代码段,以便在C++中实现高性能循环
- 在C++中,我不能在不分离标头和 cpp 的情况下实现此类吗?
- 在没有限定的情况下从 T 构造函数调用类 T 的纯虚拟实现?
- 如何在不修改C 中的LHS参数的情况下实现` `运算符
- 如何在不使用 lambda 表达式的情况下实现特定的比较器
- 如何在不必绑定到特定类的情况下实现观察者模式