- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我一直在使用下面的代码从文本文件中读取两个不同的矩阵。代码反复重新声明(至少,我认为它重新声明)局部变量 stringvalues
和 iss
。我现在才意识到变量被重新声明了——我当然不打算重新声明它们。
问题:重复重新声明这些变量有什么影响?
仅供引用:我正在使用 GCC 4.4.3 进行编译。
fstream PW3_in("./input/PW.txt", ios::in);
for(int i=0; i<900; i++)
{
PW3_in.getline(line, 450);
string stringvalues;
stringvalues = line;
istringstream iss (stringvalues,istringstream::in);
iss >> word1 >> word2 >> word3 >> word4 >> word5 >> word6 >> word7 >> word8 >> word9;
num1 = strtod(word1, NULL);
num2 = strtod(word2, NULL);
num3 = strtod(word3, NULL);
num4 = strtod(word4, NULL);
num5 = strtod(word5, NULL);
num6 = strtod(word6, NULL);
num7 = strtod(word7, NULL);
num8 = strtod(word8, NULL);
num9 = strtod(word9, NULL);
PW3[0+i*9]=num1;
PW3[1+i*9]=num2;
PW3[2+i*9]=num3;
PW3[3+i*9]=num4;
PW3[4+i*9]=num5;
PW3[5+i*9]=num6;
PW3[6+i*9]=num7;
PW3[7+i*9]=num8;
PW3[8+i*9]=num9;
}
PW3_in.close();
fstream PP3_in("./input/PP.txt", ios::in);
for(int i=0; i<900; i++)
{
PP3_in.getline(line, 450);
string stringvalues;
stringvalues = line;
istringstream iss (stringvalues,istringstream::in);
iss >> word1 >> word2 >> word3 >> word4 >> word5 >> word6 >> word7 >> word8 >> word9;
num1 = strtod(word1, NULL);
num2 = strtod(word2, NULL);
num3 = strtod(word3, NULL);
num4 = strtod(word4, NULL);
num5 = strtod(word5, NULL);
num6 = strtod(word6, NULL);
num7 = strtod(word7, NULL);
num8 = strtod(word8, NULL);
num9 = strtod(word9, NULL);
PP3[0+i*9]=num1;
PP3[1+i*9]=num2;
PP3[2+i*9]=num3;
PP3[3+i*9]=num4;
PP3[4+i*9]=num5;
PP3[5+i*9]=num6;
PP3[6+i*9]=num7;
PP3[7+i*9]=num8;
PP3[8+i*9]=num9;
}
PP3_in.close();
最佳答案
是的,它重新声明了它们。但每个声明实际上是一个不同的变量。就像您在两个不同的函数声明中调用了某些东西 x
一样。
该变量显示在 block 的开头声明。在声明它的 block 结束后,变量“超出范围”(即它被销毁并且不再存在)。
事实上,对于 for
循环的每次迭代,该变量都会被销毁并重新创建一次。每次它都有相同的名称,但在概念上是一个完全不同的变量(即使它在内存中占据相同的位置)。
此外,如果您尝试在两个 for
循环之间使用变量 stringvalues
,编译器会报错,因为该变量不存在。
因此,即使这两个变量声明声明的变量具有相同的名称,但这些变量实际上是不同的变量。您可以将第二个 block 中的那个重命名为名称末尾有一个 1
,效果将完全相同。
关于C++ 重新声明局部变量有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13354099/
有人可以解释一下为什么我得到: "use of unassigned local variable number_of_column" for: if (i f.LastWriteTime).Fir
我正在尝试为查询定义和初始化 MySQL 变量。 我有以下几点: declare @countTotal int; SET @countTotal = select COUNT(*) from nG
局部变量由小写字母或下划线(_)开头.局部变量不像全局和实变量一样在初始化前含nil值. ruby>$foo nil ruby>@foo nil ruby>foo ER
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
当我单击 Login 类上的注册按钮时,出现 nullpointerException,它给出了该错误。我尝试修改本地和全局变量,但似乎没有任何方法可以修复该错误,我可能在 onClickListen
我之前看过一些关于此的帖子,但我一直无法找到有关 actionListeners 的帖子。我正在尝试使用 JButton 数组创建井字棋。如果可能的话,如何在使用 for 循环临时变量的同时向它们添加
我试图找出一种将 getView() 方法中的位置变量传递给内部类的方法。但是,这不能是最终变量,因为 ListView 中的每个项目都会调用 getView() ,因此它会发生变化。有没有办法访问该
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
这对你们中的某些人来说似乎微不足道,但我对下面的这两个示例感到困惑。 int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int i
这个问题在这里已经有了答案: How do JavaScript closures work? (86 个答案) 关闭 7 年前。 所以我正在复习我的 vanilla Javascript,专门用于
我正在将mockito与spring(java 1.8)一起使用,并且我尝试在我的Answer对象中使用局部变量: public IProductDTO productForMock = null;
是否可以在java中为静态方法注入(inject)局部变量,比如 @Inject public void someMethod() { @MyInjectQualifier MyObjectC
我有一个函数,每 2 秒被重复调用一次,每次从屏幕顶部带来一个具有随机纹理的球。我希望能够在 touchesBegan 中使用这个球,但我不能,因为它是一个局部变量。我试过将它设为全局变量,但这给了我
这是(我假设)一个基本问题,但我似乎无法弄清楚。 给定以下代码: from src.Globals import * import pygame # Used to manage how fast t
这就是我在循环中引用全局变量的方法。 _.forEach(myTableName.detailsObjects, function (o, key) { if
我已经创建了一些代码: import numpy as np Length=(2.7)*10**-3 Nx=4 x = np.linspace(0, Length, Nx+1) # mes
如何获取局部变量? 我有这个代码 if (ctrl is Control) { Control c = (Control)ctrl; foreach (object innerCtrl
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Difference between class variables and class instance
我正在学习 Python 3,我有一个关于 Python 中面向对象编程的非常基本的问题。这是我的代码。 class pet: number_of_legs = 0 def count
我有以下代码块: class Student{ int age; //instance variable String name; //instance varia
我是一名优秀的程序员,十分优秀!