, где темп - это объект Trienode (не указатель), а дети - это не порядок_map . < /p>
Однако это вызывает ошибку сегментации или ошибки. Почему это происходит < /p>
Код: Выделить всё
class Trie_1 {
struct TrieNode_1 {
std::unordered_map children;
bool endOfWord;
};
TrieNode_1 root;
public:
Trie_1() {
}
void insert(std::string word) {
auto temp = &root;
for (char c: word) {
if (temp->children.find(c) == temp->children.end()) {
temp->children.insert(std::pair(c, TrieNode_1()));
temp = &temp->children.at(c);
} else {
temp = &temp->children.at(c);
}
}
temp->endOfWord = true;
}
bool search(std::string word) {
TrieNode_1 temp = root;
for (char c: word) {
if (temp.children.find(c) == temp.children.end()) {
return false;
}
temp = temp.children.at(c);
}
return temp.endOfWord;
}
};
< /code>
Если я замените < /p>
temp = temp.children.at(c);
< /code>
с < /p>
auto k = temp.children.at(c);
temp = k
Подробнее здесь: https://stackoverflow.com/questions/797 ... -traversal