Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 解释如何使用递归c+查找前向列表的最大值+;_C++ - Fatal编程技术网

C++ 解释如何使用递归c+查找前向列表的最大值+;

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

所以我的教授创造了这个代码,我不完全理解它,所以如果可能的话,我想解释一下到底发生了什么。我特别不理解第二段代码中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_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())