C++ 堆栈调用:为什么光标会跳到递归函数的特定位置 问题描述

C++ 堆栈调用:为什么光标会跳到递归函数的特定位置 问题描述,c++,recursion,C++,Recursion,我正在尝试解决以下问题: 执行算法打印所有有效的(例如,正确打开和 闭合)n对括号的组合。示例:输入:3 (例如,3对括号)输出:()(),()(()),(())(),(())(),((()) 当我重新研究基本情况,并从堆栈中弹出调用时,它总是跳转到代码的以下部分 cout <<"==============> RESTART HERE " << endl; cout您可能正在优化代码上运行调试器。这样做没有错,但是优化器可能已经对代码进行

我正在尝试解决以下问题:

执行算法打印所有有效的(例如,正确打开和 闭合)n对括号的组合。示例:输入:3 (例如,3对括号)输出:()(),()(()),(())(),(())(),((())


当我重新研究基本情况,并从堆栈中弹出调用时,它总是跳转到代码的以下部分

 cout <<"==============> RESTART HERE " << endl;

cout您可能正在优化代码上运行调试器。这样做没有错,但是优化器可能已经对代码进行了重新排序。如果无序执行困扰您,请在使用调试器时关闭优化器。

您可能正在对优化代码运行调试器。这样做没有错,但是优化器可能已经对代码进行了重新排序。如果无序执行困扰您,请在使用调试器时关闭优化器。

递归函数没有什么特别之处;电话一打完,你就马上回到那个地方。调用
getchar()
时,是否希望控件返回时返回到
main
的开头?我手动跟踪递归。这是正确的。我在想它应该回到开始,这真是愚蠢。谢谢@molbdniothere对于递归函数没有什么特别之处;电话一打完,你就马上回到那个地方。调用
getchar()
时,是否希望控件返回时返回到
main
的开头?我手动跟踪递归。这是正确的。我在想它应该回到开始,这真是愚蠢。谢谢你@molbdnilo
void HelloWorld(int count)
{
    if(count<1) return;
    if(count<0) return;
    cout << " Hello World!" << endl;
    HelloWorld(count - 1);
}
# include<stdio.h>
#include <iostream>

using namespace std;

# define MAX_SIZE 100

void _printParenthesis(int pos, int n, int open, int close);

/* Wrapper over _printParenthesis()*/
void printParenthesis(int n)
{
  if(n > 0)
     _printParenthesis(0, n, 0, 0);
  return;
}

void _printParenthesis(int pos, int n, int open, int close)
{
  static char str[MAX_SIZE];
  if(close == n)
  {
    cout <<" open " << open <<" close " << close <<" " << pos<< endl;
    cout << str << endl;
    return;
  }
  else
  {
    if(close < open) {
        str[pos] = '}';
        cout <<" B open " << open <<" close " << close <<" " << pos<< " }" << endl;
        _printParenthesis(pos+1, n, open, close+1);

    }
    cout <<"==============> RESTART HERE " << endl;
    if(open < n) {
       str[pos] = '{';
        cout <<" A open " << open <<" close " << close <<" " <<pos << " {"  << endl;
        _printParenthesis(pos+1, n, open+1, close);

    }
  }
}

/* driver program to test above functions */
int main()
{
  int n = 3;
  printParenthesis(n);
  getchar();
  return 0;
}