【发布时间】:2015-11-01 16:19:20
【问题描述】:
我有一个简单的数字树定义如下:
class DTN {
public:
DTN () :
is_word(false), word_to_here(""), children()
{}
DTN (bool iw, std::string wth) :
is_word(iw), word_to_here(wth), children()
{}
bool is_word;
std::string word_to_here;
Map<char,DTN> children;
};
我在定义一个名为longest_word (const DTN& dtn) 的函数时遇到问题,该函数假设返回数字树中最长的单词,并带有迭代器和递归,如下所示:
std::string longest_word (const DTN& dtn) {
std::string lw = dtn.word_to_here;
for(auto s:dtn.children){
if(s.second.is_word && lw.length()<s.second.word_to_here.length()){
lw = longest_word(s.second);
}
longest_word(s.second);
}
return lw;
}
假设我们在数字树 DTN 中有三个单词:(ante, anteater, anthebellum),调用最长的单词(DTN) 会给我一个空字符串“”而不是“anthebellum”。有人可以指出我在最长字函数中做错了什么吗?使用实际代码将不胜感激,因为我的英语不好,代码对我来说更容易理解。提前致谢。
【问题讨论】:
-
这看起来不够——儿童类型? NTN 与 DTN?
-
很抱歉,我放错了课程,我正在更新正确的课程。
标签: c++