我有以下C++代码:无法端口C++其插入到一个字典树来锈病由于多个可变代码借用
#include <vector>
#include <string>
using namespace std;
struct Trie {
bool eow; //end of word
char val;
vector<Trie> chd; //children
void push_word(const string& word){
Trie& trie = *this;
for (char c: word){
if (trie.chd.empty() || trie.chd.back().val != c) {
trie.chd.push_back(Trie{false, c, vector<Trie>{}});
}
trie = trie.chd.back();
}
trie.eow = true;
}
};
这对串的线索。 push_word
应该只接受按字典顺序大于trie中已经包含的任何单词的字符串;这样可以在每个节点上跳过搜索正确的孩子。换句话说,这使我们能够有效地从字的排序矢量构建特里结构:
Trie from_sorted_vector(const vector<string>& words){
Trie trie{false, '\0', vector<Trie>{}};
for (const auto& word: words) {
trie.push_word(word);
}
return trie;
}
在锈病我有以下:
#[derive(Eq, PartialEq, Debug, Clone)]
struct Trie {
eow: bool,
val: char,
chd: Vec<Trie>,
}
impl Trie {
fn new(eow: bool, val: char, chd: Vec<Trie>) -> Trie {
Trie {
eow: eow,
val: val,
chd: chd,
}
}
fn push_word(&mut self, word: &String) {
let mut trie = self;
for c in word.chars() {
// ???
}
}
}
我不能以类似的方式实现push_word
到C++。对于trie
或trie.chd
或trie.chd
的最后一个元素,我总是得到两个可变借入或一个不可变和一个可变借入。我想知道如何完成这个任务。
第一句话得到了赞扬,我很难过说... – 2014-11-23 07:51:52
第一句否定了本来会是加票的。 – nathanchere 2015-06-08 12:44:41
@nathanchere,即使你不喜欢我的框架,我希望它对你有所帮助! :) – huon 2015-06-09 03:18:48