C++ 解释如何使用递归c+查找前向列表的最大值+;
所以我的教授创造了这个代码,我不完全理解它,所以如果可能的话,我想解释一下到底发生了什么。我特别不理解第二段代码中temp_项和max_项的区别。。。非常感谢C++ 解释如何使用递归c+查找前向列表的最大值+;,c++,C++,所以我的教授创造了这个代码,我不完全理解它,所以如果可能的话,我想解释一下到底发生了什么。我特别不理解第二段代码中temp_项和max_项的区别。。。非常感谢 T SLL:: max() { if (empty()) throw std::range.error(“”); auto temp_item=*begin(); return max(begin(),temp_item); } T SLL:max(iterator i, T&max_i
T SLL:: max()
{
if (empty())
throw std::range.error(“”);
auto temp_item=*begin();
return max(begin(),temp_item);
}
T SLL:max(iterator i, T&max_item)
{
if (i==end())
return max_item;
if (max_item > max(next(i), temp_item))
return max_item;
else
return max(next(i), max_item);
}
temp\u项
未在max(迭代器,T&)
中定义。另外,max(迭代器,T&)
不应该像这段代码那样在自身内部被调用两次。这会冗余地迭代列表,导致性能下降。调用它一次并保存结果,然后在需要时返回结果tsll:max(迭代器i,常量T&max_项){if(i==end())返回max_项;T temp_项=max(下一个(i),max_项);if(max_项>temp_项)返回max_项;返回temp_项;}
返回*std::max_元素(begin(),end())代码>