我正在實現一個trie,它將在達到單詞末尾時打印定義。我使用字符串進行定義。但是,當我將定義分配給字符串。由於Trie中的字符串結構導致程序崩潰
#include <bits/stdc++.h>
#define ALPHABET_SIZE 26
#define CHAR_TO_INDEX(c) ((int)c - (int)'0')
using namespace std;
typedef struct trienode{
string definition; //For Definition of the word
bool isLeaf;
struct trienode *children[ALPHABET_SIZE];
}node;
node* getnode()
{
int i;
node *t=new node();
t->isLeaf=false;
for(i=0;i<26;i++)
{
t->children[i]=NULL;
}
return t;
}
void insert(node *root,string s)
{
node *crawler=root;
int level,index,length=s.length();
for(level=0;level<length;level++)
{
index= CHAR_TO_INDEX(s[level]);
if(crawler->children[index]==NULL)
{
crawler->children[index]=getnode();
}
crawler=crawler->children[index];
}
crawler->definition= "Definition of" + s; //Here is the code crashing,when I am assigning the definition
crawler->isLeaf=true;
}
請發表[mcve]。 – PaulMcKenzie
您可能會傳遞不完全大寫的字符串。 – sameerkn