4

关于C++数组类型

 3 years ago
source link: https://www.oschina.net/question/2976815_2323922
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

关于C++数组类型

#include <iostream>

// 定义字符大小
#define CHAR_SIZE 128

// 表示Trie节点的类
class Trie
{
public:
    bool isLeaf;
    Trie* character[128];

    // 构造函数
    Trie()
    {
        this->isLeaf = false;

        for (int i = 0; i < CHAR_SIZE; i++)
            this->character[i] = nullptr;
    }

    void insert(std::string);
    bool deletion(Trie*&, std::string);
    bool search(std::string);
    bool haveChildren(Trie const*);
};

// 迭代函数:在Trie中插入一个键
void Trie::insert(std::string key)
{
    // 从根节点开始
    Trie* curr = this;
    for (int i = 0; i < key.length(); i++)
    {
        // 如果路径不存在,则创建一个新节点
        if (curr->character[key[i]] == nullptr)
            curr->character[key[i]] = new Trie();

        // 转到下一个节点
        curr = curr->character[key[i]];
    }

    // 将当前节点标记为叶子
    curr->isLeaf = true;
}

不明白这个character不是Tries* 类型吗?为什么里面又可以存放字符。key[i]这种。

新手小白,不懂就问


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK