c ++递归调用不将项目推向向量 [英] c++ recursive call not pushing item to vector

查看:200
本文介绍了c ++递归调用不将项目推向向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天,我问了一个非常类似的问题,我要问递归。以下是旧问题的链接: C ++向量在递归函数中丢失数据



我现在感觉很密,因为我认为我理解这个问题,试图推送到递归调用的向量,但我现在有一个类似的功能的问题。
下面是函数的代码位:

  void TriangleDynamic :: collectRayRecursive(Ray& ray,double bin,double radius,Point& org)
{
if(getLowestLevel())
{
//这是我推光线的地方。
raysPushBack(move(ray));
}

else
{
bool foundIntersectSub = false;
unsigned int ctr = 0;
while((!foundIntersectSub)&&(ctr <= getSubTrianglesSize() - 1))
{
if(getSubTriangle(ctr).intersect(ray))
{
foundIntersectSub = true;
getSubTriangle(ctr).collectRayRecursive(ray,binSize,radius,org);

}
ctr = ctr + 1;
}
}

}

是TriangleDynamic类的一部分,并将Ray作为参数。已知Ray与TriangleDynamic对象相交,但是如果三角形不是最低级三角形,则函数通过三角形subTriangles查找哪个subTriangle也已经被Ray相交(一个将被设计相交) 。这是发生递归调用的地方,并且只有一旦达到最低级别的三角形,光线将被推向称为光线的向量,并且是三角动态的成员数据。但是我又失去了数据。有什么办法这样做吗?我不能相信我的上一个问题是多么快速和容易回答,我以为我会再试一次!感谢!

解决方案

  getSubTriangle(ctr).collectRayRecursive(ray,binSize,radius,org ); 

您的向量生活在 TriangleDynamic [as你说的],但是你的递归调用每次都在 TriangleDynamic 的不同实例上工作,因此向量第一个递归调用不同于第二个递归调用的向量,这与第三个递归调用不一样... [我不能确定它是真的,因为我需要更多的代码,但我假设是]。



您必须确保在递归调用之后,复制向量 getSubTriangle()生成的三角形中。



EDIT:查看递归调用的堆栈跟踪:

调用堆栈看起来像这样:

  | triangle_1.collectRayRecursive()| 
---------------------------------

通过调用您获得的递归调用[注意,triangle_1,triangle_2是不同的对象]

  | triangle_2.collectRayRecursive()| 
| triangle_1.collectRayRecursive()|
---------------------------------

依此类推:...

  | triangle_n。 collectRayRecursive()| 
....
| triangle_2.collectRayRecursive()|
| triangle_1.CollectRayRecursive()|
---------------------------------

现在,修改位于对象 triangle_n 向量 c $ c> [最深的递归调用],而不是其他三角形中的向量

当您从递归中返回时,您不需要复制向量 triangle_n c $ c>。

因此 - 新信息未添加到 triangle_(n-1)中的向量,不会将其传递给 triangle_(n-2) ...不会将它传递给 triangle_1 - 您期望看到的最终结果。 / p>

A few days ago, I asked a VERY similar question to the one I am about to ask about recursion. Here is the link to the old question:C++ vector loses data in recursive function

I feel pretty dense right about now, because I thought I understood the problem with trying to push to a vector in a recursive call, but I am now having issues with a similar function. Here is the bit of code for the function:

void TriangleDynamic::collectRayRecursive(Ray &ray, double binSize, double radius, Point &org)
{   
    if (getLowestLevel())
    {
        //this is where I push the ray. 
        raysPushBack(move(ray));
    }

    else
    {
        bool foundIntersectSub = false;
        unsigned int ctr = 0;
        while((!foundIntersectSub) && (ctr<=getSubTrianglesSize() - 1 ))
        {
            if (getSubTriangle(ctr).intersect(ray) )
            {
                foundIntersectSub = true;
                getSubTriangle(ctr).collectRayRecursive(ray, binSize, radius, org);

            }
            ctr = ctr + 1;
        }
    }

}

The function is part of the TriangleDynamic class and takes a Ray as a parameter. The Ray is known to intersect with the TriangleDynamic object, but if the triangle is not the "lowest level" triangle, the function looks through the triangles subTriangles to find which subTriangle has also been intersected by the Ray (one will be intersected by design). This is where the recursive call occurs, and it is only once the lowest level triangle is reached that a ray will be pushed to the vector which is called rays and is member data of TriangleDynamic. But once again I lose data. Is there any way of doing this? I can't believe how quickly and easily my previous question was answered, I thought I would try again! Thanks!

解决方案

getSubTriangle(ctr).collectRayRecursive(ray, binSize, radius, org);

Your vector lives in a TriangleDynamic [as you said], but your recursive call works on a different instance of a TriangleDynamic each time, so the vector of the first recursive call is not the same as the vector of the second recursive call, which is not the same as the 3rd recursive call.... [well, I cannot be sure about it realy since I need more code for it, but I assume so].

You will have to make sure that after the recursive call, you copy the vector which lives in the Triangle generated by getSubTriangle() to this.

EDIT: a peek on the "stack trace" of the recursive invokation:
The calls stack could look something like that:

|triangle_1.collectRayRecursive()|
---------------------------------

By invoking the recursive call you get [note that triangle_1, triangle_2 are different objects]

|triangle_2.collectRayRecursive()|
|triangle_1.collectRayRecursive()|
---------------------------------

And so on: ...

|triangle_n.collectRayRecursive()|
....
|triangle_2.collectRayRecursive()|
|triangle_1.collectRayRecursive()|
---------------------------------

Now, you modify the vector that lives inside the object triangle_n [the deepest recursive call], but not the vector in the other triangles.
When you get back from the recursion, you pop the head, which is triangle_n - without copying the vector.
As a result - the new information was not added to the vector in triangle_(n-1), which will not pass it to triangle_(n-2) ... which will not pass it to triangle_1 - where you expect to see the final results.

这篇关于c ++递归调用不将项目推向向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆