#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <stdlib.h>
using namespace std;

struct trienode
{
    map<char, struct trienode*> m;
    bool endofword;
};

struct trienode* create_node()
{
    struct trienode* new_node = (struct trienode*)malloc(sizeof(struct trienode));
    new_node->endofword = false;
    return new_node;
};

void word_insert(struct trienode* root, string word)
{
    struct trienode* curr = root;
    for (unsigned int i =0; i < word.size(); i++) {
        if (curr->m.find(word[i]) == curr->m.end()) {
            struct trienode* new_node = create_node();
            ***curr->m.insert(pair<char, struct trienode*>(word[i], new_node) );***

        }
        curr = curr->m.find(word[i])->second;
    }
    curr->endofword = true;
}

int main()
{
    struct trienode* root = NULL;
    root = create_node();

    vector<string> v = {"aspirin", "aspetol", "astray", "atran", "chronic"};
    for (unsigned int i =0; i < v.size(); i++) {
        word_insert(root, v[i]);
    }
}

我正在尝试构建一个trie数据结构来保存一组字符串 . 我写了一个word_insert()函数来将一个单词插入到trie中 . 为了在trie中插入一个单词,我从根节点开始,查看根节点中的映射是否包含char,如果是,则继续执行下一个charachter . 如果char不在节点的映射中,我创建另一个trienode并在 Map 中插入一个条目 . 但是,当我这样做时,我发现我的代码存在问题 . 我的代码挂起在我尝试将(char,struct trienode *)对插入到 Map 中 .

有人能告诉我这有什么问题吗?谢谢 .