Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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++ trie的以下代码中发生了什么_C++_Algorithm_Trie - Fatal编程技术网

C++ trie的以下代码中发生了什么

C++ trie的以下代码中发生了什么,c++,algorithm,trie,C++,Algorithm,Trie,我正在检查这个Trie,我很难理解下面的代码 void Trie::addWord(string word) { Node * currentNode = root; for (int i = 0; i < word.size(); ++i) { char currentChar = tolower(word.at(i)); int index = currentChar - 'a'; assert(index &g

我正在检查这个Trie,我很难理解下面的代码

void Trie::addWord(string word)
{
    Node * currentNode = root;

    for (int i = 0; i < word.size(); ++i)
    {
        char currentChar = tolower(word.at(i));
        int index = currentChar - 'a';
        assert(index >= 0);     // Makes sure the character is between a-z
        if (currentNode->children[index] != NULL)
        {
            // check if the current node has the current character as one of its decendants
            currentNode = currentNode->children[index];
        }
        else
        {
            // the current node doesn't have the current character as one of its decendants
            Node * newNode = new Node(currentChar);
            currentNode->children[index] = newNode;
            currentNode = newNode;
        }
        if (i == word.size() - 1)
        {
            // the last character of the word has been reached
            currentNode->end = true;
        }
    }
}

它会给你字母位置在字母表A中作为位置0。这里把A转换成ASCII码,从A Z字母中减去,变得更容易处理。

< P>它将把字母位置放在字母表中,把A视为位置0。在这里,a被转换为ASCII码,从a-z字母开始,a-z字母被分为两个字符,这使得处理起来更加容易。

人们往往忘记的是,字符是一个包含ASCII值的字节

所以我认为ascii表中的'a'是97,所以所有的工作都在进行中,就是currentChar-97

请按照实际值

但要记住,字符就是字节,你可以经常使用这个事实! 我使用它跳过指针中的内存地址,例如:

void* a = (void*)((char*)arr+2);
此代码将返回arr+两个字节的内存,同时忽略arr中保存的类型


请注意-|‘a’-‘a’|=|‘a’-‘a’|,但一个是负数,另一个是正数,这是人们容易忘记的-字符是包含ascii值的字节

所以我认为ascii表中的'a'是97,所以所有的工作都在进行中,就是currentChar-97

请按照实际值

但要记住,字符就是字节,你可以经常使用这个事实! 我使用它跳过指针中的内存地址,例如:

void* a = (void*)((char*)arr+2);
此代码将返回arr+两个字节的内存,同时忽略arr中保存的类型


请注意-|'a'-'a'|=|'a'-'a'|,但其中一个为负,而另一个为正;这是为了检查字符,因为用户可以输入一个不在a到z之间的字符。

int index=currentChar-'a';这是为了检查字符,因为用户可以在int index=currentChar-'a'行输入一个不在a到z之间的字符。

;currentChar(无论是什么)将被ASCII值为97的“a”字符减去。 在这种情况下,您有两个条件:

首先,如果currentChar在a-z之间,则索引的结果将始终大于等于0

否则,currentChar不在a-z之间,因为索引将是负数,currentChar不能在a-z之间,因为tolower函数

您可以参考此内容以了解有关ASCII值的更多信息

您还需要更新条件assertindex>=0&&index<26,因为{,},|和~将在int index=currentChar-'a'行处使索引>=0

;currentChar(无论是什么)将被ASCII值为97的“a”字符减去。 在这种情况下,您有两个条件:

首先,如果currentChar在a-z之间,则索引的结果将始终大于等于0

否则,currentChar不在a-z之间,因为索引将是负数,currentChar不能在a-z之间,因为tolower函数

您可以参考此内容以了解有关ASCII值的更多信息

此外,您还需要更新条件assertindex>=0&&index<26,因为{,},|和~将使索引>=0

它应该是assertindex>=0&&index<26,以确保它位于a-z之间,因为{and}也将产生>=0。它应该是assertindex>=0&&index<26,以确保它位于a-z之间,因为{and}也将产生>=0。