简单的递归问题
假设我们有一个简单的递归。
int x(int a){
if(a<10)
x(a+1);
else
!STOP!
b++;
return b;
}
Globaly:
int b=0;
在 main 中我们可以有这样的东西:
int p=x(1);
有没有办法停止递归,使 p 为 0,这意味着“b++”永远不会被执行。
如果您能告诉我一些代替 !STOP! 的表达式,我将不胜感激。
但是,我不想要这样的事情,我只想停止递归,比如break;在 while() 循环中执行...:
int ok=0;
int x(int a){
if(a<10)
x(a+1);
else
ok=1;
if(ok==0)
b++;
return b;
}
如果对问题有任何不清楚的地方,请询问。
Let's say we have a simple recursion like.
int x(int a){
if(a<10)
x(a+1);
else
!STOP!
b++;
return b;
}
Globaly:
int b=0;
In main we could have something like this:
int p=x(1);
Is there any way to stop the recursion so that the p will be 0, this means that "b++" will never be executed.
I'll be grateful if you could tell me some expresion to put instead of the !STOP!
But, I don't want anything like this, I just want to stop the recursion, like break; does in a while() loop...:
int ok=0;
int x(int a){
if(a<10)
x(a+1);
else
ok=1;
if(ok==0)
b++;
return b;
}
If there's anything unclear about the question, just ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你为什么不这样做呢?
但问题是,您正在递归例程中修改全局变量,这并不是特别线程安全并且相当草率。您返回的值始终被忽略,除了顶级调用者之外。您还正在做一些最好在循环中完成的事情(但我假设您的实际情况比这更大,或者您是一名学生)。
你不能真正“打破”递归——返回足够好。在老式 C 中,您可能会使用 setjmp/longjmp (及其所有危险 - 换句话说,不要),而在 C++ 中,您可能会使用 try/catch/ throw,这也会展开堆栈。
Why wouldn't you do this?
The thing is, though, you're modifying a global in a recursive routine, which is not especially threadsafe and pretty sloppy. You're returning a value that is always ignored except by the top level caller. You're also doing something that is better off being done in a loop (but I assume that your actual case is bigger than this, or you're a student).
You can't really "break" the recursion - returning unwinds well enough. In oldey-timey C you might use setjmp/longjmp (and all its perils - in other words, DON'T), and in C++ you might use try/catch/throw, which will unwind the stack as well.
像这样怎么样?
How about like this?
C++ 中唯一能像这样展开堆栈的就是异常。还有
setjmp()/longjmp()
,但它们永远不应该在 C++ 程序中使用。任何其他构造最多可以从当前函数返回。The only thing in C++ that will unwind the stack like that is an exception. There's also
setjmp()/longjmp()
, but those should never be used in a C++ program. Any other construct can at most return from the current function.回来怎么样?
我认为这看起来更好一点
编辑:
我认为您可以使用异常机制来展开堆栈并到达第一次调用点,但输入
main()。在
x
中引用b
,给定代码:表明
x
用于初始化某些全局变量,并且可以在main之前执行()
。如何使用一些辅助函数,将x
的调用包装在 try - catch 块中,并在 |STOP| 的位置引发异常?How about returning?
I think this looks a bit better
EDIT:
I think You could use exception mechanism to unwind the stack and get to the point of first invocation, but it's safe after entering
main()
. Referencingb
inx
, given the code:suggests that
x
is used for initialization of some global variable and may be executed beforemain()
. How about using some helper function that wraps invocation ofx
in a try - catch block and throwing an exception in the place of |STOP|?如果您尝试在
main()
中声明b
,并在x()
中使用b
,则有一开始就已经出了问题。相反,通过将b
作为参数传递给x
并返回b
的修改版本,使b
成为局部变量。If you're trying to declare
b
inmain()
, and useb
inx()
then there's something wrong already to begin with. Instead, makeb
into a local variable by passing it as a parameter tox
, and returning a modified version ofb
.我不太喜欢使用异常进行控制。我不希望通过使用异常而不是 if/return 语句来节省很多周期。无论如何,在抛出异常之前,您都必须测试边界条件。
但是,您可以通过更改函数的返回类型来稍微简化问题。
I'm not a big fan of using an Exception for control. I don't expect you'll save many cycles by using Exceptions instead of if/return statements. You're going to have to test your boundary conditions anyway before throwing an Exception.
You can however simplify the problem a bit by changing the return type of the function.