c++矢量未在嵌套 for 循环中更新

发布于 2024-11-18 02:53:10 字数 2047 浏览 1 评论 0 原文

因此,我创建一个向量(大小为 nmask+3)并将其初始化为 0,然后为其中一个元素分配一个初始值。然后,我创建一个 for 循环,遍历向量的前 nmask 元素,并将向量中平均 26 个其他元素分配给每个元素(由 4D int 数组 voxt 定义,其中包含向量地址)。

我的问题是,当我检查嵌套循环(第一个 cout)内向量(phi)中的非零元素的值时,这些值很好并且符合我的预期。但是,当循环完成所有 nmask 元素(for (int i= 0; i 退出)时,我再次检查 phi 的非零元素,它们全部丢失(重置为 0),最后一个非零元素(以及手动设置为 1 的元素 tvox)除外。

我觉得由于 phi 是在所有循环之外初始化的,所以不应该重置值,并且嵌套循环内的任何更新的元素都应该在循环退出时保持更新。关于发生了什么/如何解决这个问题有什么想法吗?代码如下;我试图对我得到的输出进行评论。提前致谢。

vector<double> phi(nmask+3, 0); //vector with nmask+3 elements all set to 0 (nmask = 13622)
    phi[tvox]= 1; //tvox is predefined address (7666)

    for (int n= 0; n<1; n++)
    {
        vector<double> tempPhi(phi); //copy phi to tempPhi

        for (int i= 0; i<nmask; i++)
        {
            for (int a= -1; a<=1; a++)
            {
                for (int b= -1; b<=1; b++)
                {
                    for (int c= -1; c<=1; c++)
                    {
                        if (!(a==0 && b==0 && c==0))
                        {
                            //oneD26 is just (double) 1/26
                            phi[i]= tempPhi[i]+oneD26*tempPhi[voxt[i][1+a][1+b][1+c]];
                            if (phi[i]!=0)
                            {
                                //this gives expected results: 27 nonzero elements (including tvox)
                                cout << n << " " << i << " " << a << b << c << " " << phi[i] << endl;
                            }
                        }
                    }
                }
            }
        }

        phi[svox]= 0; //svox = 7681
        phi[tvox]= 1;

        for (int q= 0; q<nmask; q++)
        {
            //this gives only 2 nonzero values: phi[tvox] and phi[9642], which was the last nonzero value from 1st cout
            if (phi[q]!=0)
                cout << q << " " << phi[q] << endl;
        }

    }

So I create and initialize a vector (of size nmask+3) to 0, and I assign an initial value to one of the elements. I then make a for loop that goes through the first nmask elements of the vector and assigns to each element an average of 26 other elements in the vector (defined by the 4D int array voxt, which contains vector addresses).

My problem is that when I check the values of nonzero elements in my vector (phi) within the nested loop (the first cout), the values are fine and what I expect. However, when the loop finishes going through all nmask elements (for (int i= 0; i<nmask; i++) exits), I check the nonzero elements of phi again, and they are all lost (reset to 0) except for the last non-zero element (and element tvox which is manually set to 1).

I feel that since phi is initialized outside of all the loops, there should be no resetting of values going on, and that any updated elements within the nested loop should remain updated upon exit of the loop. Any ideas as to what is going on / how to fix this? Code is below; I tried to comment in a sense of the outputs I'm getting. Thanks in advance.

vector<double> phi(nmask+3, 0); //vector with nmask+3 elements all set to 0 (nmask = 13622)
    phi[tvox]= 1; //tvox is predefined address (7666)

    for (int n= 0; n<1; n++)
    {
        vector<double> tempPhi(phi); //copy phi to tempPhi

        for (int i= 0; i<nmask; i++)
        {
            for (int a= -1; a<=1; a++)
            {
                for (int b= -1; b<=1; b++)
                {
                    for (int c= -1; c<=1; c++)
                    {
                        if (!(a==0 && b==0 && c==0))
                        {
                            //oneD26 is just (double) 1/26
                            phi[i]= tempPhi[i]+oneD26*tempPhi[voxt[i][1+a][1+b][1+c]];
                            if (phi[i]!=0)
                            {
                                //this gives expected results: 27 nonzero elements (including tvox)
                                cout << n << " " << i << " " << a << b << c << " " << phi[i] << endl;
                            }
                        }
                    }
                }
            }
        }

        phi[svox]= 0; //svox = 7681
        phi[tvox]= 1;

        for (int q= 0; q<nmask; q++)
        {
            //this gives only 2 nonzero values: phi[tvox] and phi[9642], which was the last nonzero value from 1st cout
            if (phi[q]!=0)
                cout << q << " " << phi[q] << endl;
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无人接听 2024-11-25 02:53:10

很难说到底发生了什么,但最简单的解释是,在 phi[i] 设置为非零并显示到 cout 后,它在内部循环的后续迭代之一中再次设置为零。

Difficult to tell just what is going on, but the easiest explanation is that after phi[i] gets set to non-zero and displayed to cout, it gets set to zero again in one of the later iterations through the inner loops.

恋你朝朝暮暮 2024-11-25 02:53:10

如果您在更新之前进行一些跟踪并检查 phi[i],您会发现您经常用零覆盖非零元素。

注意:我不知道你的代码做了什么,这是纯粹的福尔摩斯推理。如果在循环之后你只发现 2 个非零元素,那么唯一的逻辑结果是在循环稍后将某些内容更新为非零之后,你将其更新为零。

If you do some tracing and check phi[i] just before updating you'll see that you often overwrite a non-zero element with zero.

Note: I have no idea what your code does, this is pure Sherlock Holmes reasoning.. if after the loops you find only 2 non-zero elements then the only logical consequence is that after updating something to non-zero later in the loop you update it to zero.

云巢 2024-11-25 02:53:10
phi[i]= tempPhi[i]+oneD26*tempPhi[voxt[i][1+a][1+b][1+c]];

使用 a、b 和 c 的嵌套 for 循环运行了 9 次组合迭代,且 i 值相同。由于每次都将 phi[i] 覆盖为新值,因此仅保留最后一次迭代的值,其中 a 和 c 均为 1。如果最后一次迭代恰好产生零值,则 phi[i] 将有很多值零。也许您打算做类似 phi[i] += ... 而不是 phi[i] = ... 的事情?

phi[i]= tempPhi[i]+oneD26*tempPhi[voxt[i][1+a][1+b][1+c]];

The nested for-loops using a, b, and c run for a combined 9 iterations with the same value of i. Since you overwrite phi[i] to a new value every time, you only retain the value from the last iteration where a, and c are all 1. If that last iteration happens to produce zero values, then phi[i] will have lots of zeroes. Perhaps you meant to do something like phi[i] += ... instead of phi[i] = ...?

软糖 2024-11-25 02:53:10

我确实建议用类似的内容替换循环的核心内容

const boost::irange domain(-1,2);
for (int i: boost::irange(0, nmask)) for (int a: domain) for (int b: domain) for (int c: domain)
{
    if (a==0 && b==0 && c==0)
        continue;
    //oneD26 is just (double) 1/26
    phi[i]= tempPhi[i]+oneD26*tempPhi[voxt[i][1+a][1+b][1+c]];
    if (phi[i]!=0)
    {
        //this gives expected results: 27 nonzero elements (including tvox)
        cout << n << " " << i << " " << a << b << c << " " << phi[i] << endl;
    }
}

当然,为了简洁起见,我假设boost/range.hpp和c++0x编译器都是如此。但是,通过简单的宏,您可以实现相同的效果。这是没有编写/使用适当的组合算法(为什么它不在标准中)。

I do suggest to replace the meat of the loop with something like

const boost::irange domain(-1,2);
for (int i: boost::irange(0, nmask)) for (int a: domain) for (int b: domain) for (int c: domain)
{
    if (a==0 && b==0 && c==0)
        continue;
    //oneD26 is just (double) 1/26
    phi[i]= tempPhi[i]+oneD26*tempPhi[voxt[i][1+a][1+b][1+c]];
    if (phi[i]!=0)
    {
        //this gives expected results: 27 nonzero elements (including tvox)
        cout << n << " " << i << " " << a << b << c << " " << phi[i] << endl;
    }
}

Of course, for brevity I assume both boost/range.hpp and c++0x compiler. However, with trivial macro's you can achieve the same. That is without writing/using a proper combinations algorithm (why is that not in the standard, anyway).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文