我想插入一个字符串,并使用trie数据结构进行搜索运行。这是我第一次使用指针的实现,所以我真的很困惑我在代码中做了什么错误,它给编译错误。请帮助调试它,并请告诉我在我的指针逻辑中出了什么问题。错误执行Trie树
typedef struct trie {
unordered_multimap<char, struct trie> child;
bool isEnd;
} trie;
trie* newtrienode()
{
trie* newnode = (trie*)malloc(sizeof(trie));
newnode->isEnd = false;
return newnode;
}
trie* root = newtrienode();
void insert(string word)
{
trie* current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word[i];
trie* node = current->child[ch];
if (node == NULL) {
trie* node = newtrienode();
current->child.insert(pair<char, trie>(ch, node));
}
current = node;
}
current->isEnd = true;
}
bool search(string word)
{
trie* current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word[i];
trie* node = current->child[ch];
if (node == NULL) {
return false;
}
current = node;
}
return true;
}
什么是编译错误? – Sabuncu
@Sabuncu如果你编译它并且看到它们会更好,因为它是一个很长的错误线程。把这个错误留在这里会使它很难阅读,我希望你明白。 – query
a)你应该发布错误,以便为未来的读者提供一个很好的问题。 b)代码不是一个完整的最小例子。 – drescherjm