C++11 (C+;+;)递归函数-don';我不知道为什么会减少 我只是从递归函数开始,了解C++的一些工作原理,但是我真的不理解这个代码是如何工作的: void q9_draw_triangle(int a, int b) { // a is the starting point, b is the end if (a > b) return; // print pattern first for(int i = 0; i < a; i++){ std::cout << "-"; } std::cout << std::endl; // call on triangle where a is incremented once q9_draw_triangle(a+1,b); // print pattern last for(int i = 0; i < a; i++){ std::cout << "-"; } std::cout << std::endl; } void q9_-draw_三角形(inta,intb){ //a是起点,b是终点 如果(a>b)返回; //先打印图案 for(int i=0;i5,然后函数提前返回,并返回到调用它的函数(父函数)。然后父函数运行其代码直到返回,从而返回到其父函数,依此类推
满足某些条件后,例如a>5,然后函数提前返回,并返回到调用它的函数(父函数)。然后父函数运行其代码直到返回,从而返回到其父函数,依此类推 有关递归工作原理的示例,请尝试在谷歌上搜索。这看起来是一个很好的起点 我已经对代码进行了注释,以解释发生了什么。这对我来说是有意义的,但我希望它对您也有意义C++11 (C+;+;)递归函数-don';我不知道为什么会减少 我只是从递归函数开始,了解C++的一些工作原理,但是我真的不理解这个代码是如何工作的: void q9_draw_triangle(int a, int b) { // a is the starting point, b is the end if (a > b) return; // print pattern first for(int i = 0; i < a; i++){ std::cout << "-"; } std::cout << std::endl; // call on triangle where a is incremented once q9_draw_triangle(a+1,b); // print pattern last for(int i = 0; i < a; i++){ std::cout << "-"; } std::cout << std::endl; } void q9_-draw_三角形(inta,intb){ //a是起点,b是终点 如果(a>b)返回; //先打印图案 for(int i=0;i5,然后函数提前返回,并返回到调用它的函数(父函数)。然后父函数运行其代码直到返回,从而返回到其父函数,依此类推,c++11,recursion,C++11,Recursion,满足某些条件后,例如a>5,然后函数提前返回,并返回到调用它的函数(父函数)。然后父函数运行其代码直到返回,从而返回到其父函数,依此类推 有关递归工作原理的示例,请尝试在谷歌上搜索。这看起来是一个很好的起点 我已经对代码进行了注释,以解释发生了什么。这对我来说是有意义的,但我希望它对您也有意义 void q9_draw_triangle(int a, int b) { // this is our condition; this states when to stop the rec
void q9_draw_triangle(int a, int b) {
// this is our condition; this states when to stop the recursion
if (a > b) return;
// print current line
// this loop draws the horizontal lines
// if a = 3, then it will draw ---
for(int i = 0; i < a; i++){
std::cout << "-";
}
std::cout << std::endl;
// now that the current line was drawn, draw the next line
// so if a = 3, now in the next function a will be 4
// so the next function will print ----, which gives us an output of
// ---
// ----
q9_draw_triangle(a+1,b); // this jumps back up to the top of the function and reruns the function with a+1 until a > b.
// once a > b, the functions return one by one and the follwoing loop is run
// in our example, a was 3 and then 4. If b was 4, then in our next iteration a would be 5,
// and the recursive function would simply return because a > b.
// this code is run once the child function returns. So a would be 4 again, printing ----
// this gives an output of
// ---
// ----
// ----
for(int i = 0; i < a; i++){
std::cout << "-";
}
std::cout << std::endl;
// here the function retunrs again, so the we get back to the second loop
// and a will be 3 again.
// so the total output is
// ---
// ----
// ----
// ---
}
void q9_-draw_三角形(inta,intb){
//这是我们的条件;它说明何时停止递归
如果(a>b)返回;
//打印当前行
//此循环绘制水平线
//如果a=3,则它将绘制---
for(int i=0;ib。
//这段代码在子函数返回后运行。所以a将再次为4,正在打印----
//这给出了
// ---
// ----
// ----
for(int i=0;i当一个函数调用它自己的时候,递归就是一个函数的层次结构。如果我定义了一个递归函数void func(int a),那么层次结构应该是这样的
func(1)
- func(2)
- func(3)
- ...
满足某些条件后,例如a>5,然后函数提前返回,并返回到调用它的函数(父函数)。然后父函数运行其代码直到返回,从而返回到其父函数,依此类推
有关递归工作原理的示例,请尝试在谷歌上搜索。这看起来是一个很好的起点
我已经对代码进行了注释,以解释发生了什么。这对我来说是有意义的,但我希望它对您也有意义
void q9_draw_triangle(int a, int b) {
// this is our condition; this states when to stop the recursion
if (a > b) return;
// print current line
// this loop draws the horizontal lines
// if a = 3, then it will draw ---
for(int i = 0; i < a; i++){
std::cout << "-";
}
std::cout << std::endl;
// now that the current line was drawn, draw the next line
// so if a = 3, now in the next function a will be 4
// so the next function will print ----, which gives us an output of
// ---
// ----
q9_draw_triangle(a+1,b); // this jumps back up to the top of the function and reruns the function with a+1 until a > b.
// once a > b, the functions return one by one and the follwoing loop is run
// in our example, a was 3 and then 4. If b was 4, then in our next iteration a would be 5,
// and the recursive function would simply return because a > b.
// this code is run once the child function returns. So a would be 4 again, printing ----
// this gives an output of
// ---
// ----
// ----
for(int i = 0; i < a; i++){
std::cout << "-";
}
std::cout << std::endl;
// here the function retunrs again, so the we get back to the second loop
// and a will be 3 again.
// so the total output is
// ---
// ----
// ----
// ---
}
void q9_-draw_三角形(inta,intb){
//这是我们的条件;它说明何时停止递归
如果(a>b)返回;
//打印当前行
//此循环绘制水平线
//如果a=3,则它将绘制---
for(int i=0;ib。
//这段代码在子函数返回后运行。所以a将再次为4,正在打印----
//这给出了
// ---
// ----
// ----
for(int i=0;istd::cout让我们把这个函数的结构看作
print_block(a)
recursive_call(a+1, b)
print_block(a)
这种结构使得每个递归级别的“sandwitch”都是连续的。例如,如果我们有a=1,b=2
,则生成的调用序列将是
print_block(1)
print_block(2)
recursion stops: 3>2
print_block(2)
print_block(1)
当递归停止时,最里面的调用a=3
返回,没有任何打印或其他调用。然后,控件将传递给它的调用者,即a=2
,它将打印并返回,将控件传递给a=1
,它将打印并返回给原始调用者(可能是main
)。您可以看到为什么a
似乎减少了:我们现在以相反的顺序遍历递归。让我们将此函数的结构视为
print_block(a)
recursive_call(a+1, b)
print_block(a)
这种结构使得每个递归级别的“sandwitch”都是连续的。例如,如果我们有a=1,b=2
,则生成的调用序列将是
print_block(1)
print_block(2)
recursion stops: 3>2
print_block(2)
print_block(1)
当递归停止时,最里面的调用a=3
返回,没有任何打印或其他调用。然后,控件将传递给它的调用者,即a=2
,它将打印并返回,将控件传递给a=1
,它将打印并返回给原始调用者(可能是main
)。您可以看到为什么a
似乎减少了:我们现在以相反的顺序遍历递归。我会小心处理调试器中正在发生的事情,尤其是在启用优化的情况下。现代编译器可以显著地重新排列代码,并且可能需要相当多的思考来重建当前的代码即使对于有经验的程序员也是如此。如果你打开了优化,试着关闭它,但对于这种情况,我会先添加额外的输出来显示调用和返回边界。你可以想象从q9_-draw_三角形(a+1,b)返回时发生的递减
恢复a的上一个绑定,就像从内部调用的绑定中减去1。您所说的“此递减”是什么意思?我会小心处理调试器中发生的事情,尤其是在启用优化的情况下。现代编译器可以显著地重新排列代码,甚至对于有经验的程序员来说,重建正在发生的事情也需要相当多的思考。如果您启用了优化,请尝试将其关闭,但对于这种情况,我将从ad开始输入额外的输出以显示调用和返回边界。您可以想象,从q9\u draw\u triangle(a+1,b)
恢复a的先前绑定时,返回会发生递减,这就像从内部调用的绑定中减去1一样。您所说的“此递减”是什么意思?