c++矢量未在嵌套 for 循环中更新
因此,我创建一个向量(大小为 nmask+3)并将其初始化为 0,然后为其中一个元素分配一个初始值。然后,我创建一个 for 循环,遍历向量的前 nmask 元素,并将向量中平均 26 个其他元素分配给每个元素(由 4D int 数组 voxt 定义,其中包含向量地址)。
我的问题是,当我检查嵌套循环(第一个 cout)内向量(phi)中的非零元素的值时,这些值很好并且符合我的预期。但是,当循环完成所有 nmask 元素(for (int i= 0; i
我觉得由于 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
很难说到底发生了什么,但最简单的解释是,在 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.
如果您在更新之前进行一些跟踪并检查
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.
使用 a、b 和 c 的嵌套 for 循环运行了 9 次组合迭代,且 i 值相同。由于每次都将 phi[i] 覆盖为新值,因此仅保留最后一次迭代的值,其中 a 和 c 均为 1。如果最后一次迭代恰好产生零值,则 phi[i] 将有很多值零。也许您打算做类似 phi[i] += ... 而不是 phi[i] = ... 的事情?
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] = ...?
我确实建议用类似的内容替换循环的核心内容
当然,为了简洁起见,我假设boost/range.hpp和c++0x编译器都是如此。但是,通过简单的宏,您可以实现相同的效果。这是没有编写/使用适当的组合算法(为什么它不在标准中)。
I do suggest to replace the meat of the loop with something like
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 propercombinations
algorithm (why is that not in the standard, anyway).