指针在传入函数时丢失信息

时间:2018-01-22 20:49:10

标签: c++ arrays pointers

编辑,我会尝试给出一个更好的例子:

首先我会告诉你有关该计划的信息: 基本上,有两个阶段,第一个是插入单词(完美地工作)

struct trieLeaf {
    keyType age = 1;
};
class Trie 
{
private:
    dataType character;
    trieLeaf *leaf = nullptr;
    class Trie **alphabet = nullptr;


int main()
{
    string input;
    Trie dictionary('\0');
    while (getline(cin, input) && input[0] != '.')
    {
            dictionary.analyzeText(input);
    }
    while (getline(cin, input) && input[0] != '.')
    {
        dictionary.approxFind(input);
    }
    system("pause>null");
}

第二阶段正在搜索单词,如果找到单词,那么我需要将其添加的次数减少1。 如果已添加的次数为0,则将其删除。

    void Trie::approxFind(string &word)
{
    int index = 0;
    string result;
    Trie *curr;
    if (curr=find(word))
    {
        cout << curr->leaf->age << endl;
        curr->leaf->age -= 1;
        if (curr->leaf->age == 0)
        {
            remove(word);
        }
    }
    else
    {
        result = approximate(word);
        cout << "Did you mean " << result << "?\n";
    }
}
Trie* Trie::find(const string &word)
{
    int index = 0;
    Trie *curr = this;
    while (word[index] != '\0')
    {
        if (curr->alphabet[word[index] - 'a'] != nullptr)
        {
            curr = curr->alphabet[word[index++] - 'a'];
        }
        else
        {
            return (nullptr);
        }
    }
    if (curr->leaf != nullptr)
    {
        return (curr);
    }
}
void Trie::remove(const string &word)
{
    int index = 0;
    Trie *curr = this, **tempArr, *temp;
    while (word[index] != '\0')
    {
        tempArr = curr->alphabet;
        temp = curr;
        curr = curr->alphabet[word[++index] - 'a'];
        if (!isArrEmpty(tempArr))
        {
            delete(tempArr);
        }
        delete(temp);
        temp = nullptr;
    }
}
bool Trie::isArrEmpty(Trie **alphabet)
{
    for (int index = 0; index < 26; index++)
    {
        if (!alphabet[index])
        {
            return (true);
        }
    }
    return(false);
}

isArrEmpty中的TempArr正在丢失它传递给funcion时指向的对象。 我希望它能更好地理解为什么

0 个答案:

没有答案
相关问题