Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的字典里没有这些词(c+;+;trie)?_C++_Linux - Fatal编程技术网

C++ 为什么我的字典里没有这些词(c+;+;trie)?

C++ 为什么我的字典里没有这些词(c+;+;trie)?,c++,linux,C++,Linux,我认为我的trie工具有问题。我用苹果这个词来测试它。虽然我的测试文件dict.txt包含单词apple,但它返回false。什么是臭虫 class Trie{ private: class Node{ public: Node* next[26]; bool isWord; Node(){ isWord = false; } }; Node* root; public: Trie() {

我认为我的trie工具有问题。我用苹果这个词来测试它。虽然我的测试文件
dict.txt
包含单词
apple
,但它返回
false
。什么是臭虫

class Trie{

private:

    class Node{

    public:
        Node* next[26];
        bool isWord;
        Node(){ isWord = false; }
    };
    Node* root;

public:

    Trie() { 
        root = new Node(); 
    }

    void load(const string& line) {

        Node* node  = root;

        for(int i = 0; i < line.size(); i++){
            char x = line[i];
            if(node->next[x-'a'] == nullptr)                    
                node->next[x-'a'] = new Node();
            node = node->next[x-'a'];
        }

        node->isWord = true;
    }

    bool contains(const string& word) {
        Node* node = root;

        for(int i = 0; i < word.size(); i++){
            char x = word[i];
            if(node->next[x-'a'] == nullptr)
                return false;
            else
                node = node->next[x-'a'];
        }

        return node->isWord;
    }

    bool startWith(const string& prefix) {
        Node* node = root;

        for(int i = 0; i < prefix.size(); i++){
            char x = prefix[i];
            if(node->next[x-'a'] == nullptr)
                return false;
            else
                node = node->next[x-'a'];
        }
        return true;
    }
};


int main() {

    Trie trie;

    ifstream inFile;
    string line;
    while(getline(inFile, line)){
        trie.load(line);
    }
    cout << trie.contains("apple") << endl;
    cout << trie.startWith("cata") << endl;

    return 0;
}
class-Trie{
私人:
类节点{
公众:
节点*next[26];
布尔语;
Node(){isWord=false;}
};
节点*根;
公众:
Trie(){
根=新节点();
}
无效荷载(常数串和线){
节点*节点=根;
对于(int i=0;i下一步[x-'a']==nullptr)
节点->下一步[x-'a']=新节点();
节点=节点->下一步[x-'a'];
}
node->isWord=true;
}
布尔包含(常量字符串和单词){
节点*节点=根;
for(int i=0;i下一步[x-'a']==nullptr)
返回false;
其他的
节点=节点->下一步[x-'a'];
}
返回节点->isWord;
}
bool startWith(常量字符串和前缀){
节点*节点=根;
对于(int i=0;i下一步[x-'a']==nullptr)
返回false;
其他的
节点=节点->下一步[x-'a'];
}
返回true;
}
};
int main(){
崔崔;
河流充填;
弦线;
while(getline(infle,line)){
三重载荷(线);
}

你忘了初始化指针了吗
nullptr
s。因此,单词可能添加不正确。但不确定。

使用调试器并单步检查代码。是的,代码检查是一件好事,但我认为使用调试器可以比等待StackOverflow上的答复更快地解决此问题。您忘记初始化
next
,给出你是一个未定义的程序。是的,你是对的,我应该初始化它们,谢谢!!请检查我的建议。我仍然不确定这是否是你的原因。嗨,事情是这样的,我的测试字典(dict.txt)实际上不包含“apple”这个词.然后我尝试了dict.txt中包含的一些单词来测试我的轮胎,它成功了!但令我惊讶的是,无论指针是否初始化,它总是有效的。