我想将数据存储在2D数组t [0] [0]的第一个索引的第一个元素中! 我写这段代码:
int main()
{
string input;
cout << "Enter string of code:\n";
cin >> input;
queue < string > m;
string t[100][3];
while (input != "^")
{
int i = 0;
t[i][0] = input;
m.push(t[i][0]);
if (input == " ")
{
t[i + 1][0];
break;
}
cin >> input;
i++;
}
int c = 1;
while (!m.empty())
{
int i = 0;
t[i][0] = m.front();
string temp;
temp = t[i][0];
t[i][1] = check(temp);
//cout <<c<<" "<<t[i][0]<<" Is: " << t[i][1] << endl;
c++;
m.pop();
i++;
}
cout << endl;
cout << c << " " << t[0][0] << " Is: " << t[0][1] << endl;
system("Pause");
return 0;
}
我对此声明有疑问
cout&lt;&lt; c&lt;&lt; “”&lt;&lt; t [0] [0]&lt;&lt; “是:”&lt;&lt; t [0] [1]&lt;&lt; ENDL;
这不会打印数组中的值!
答案 0 :(得分:0)
在while循环上声明并初始化 i
,如下所示:
int i = 0;
while (!m.empty())
{
t[i][0] = m.front();
...
让我解释一下(在你的代码中)会发生什么:
while (!m.empty())
{
int i = 0;
t[i][0] = m.front();
string temp;
temp = t[i][0];
t[i][1] = check(temp);
//cout <<c<<" "<<t[i][0]<<" Is: " << t[i][1] << endl;
c++;
m.pop();
i++;
}
每次时间进入循环体,声明一个名为i
的变量并初始化为0. i
的范围是循环的主体。
在循环结束时递增i
,但是一旦循环再次运行,i
将再次初始化为0(您也重新声明它)。因此,如果我们处于第二个循环中,您希望i
的值为1,但由于每次进入循环时初始化为0,它将始终具有在执行循环体时,值为0。
此外,这一行:
t[i + 1][0];
应发出此类警告:
main.cpp:22:23: warning: expression result unused [-Wunused-value]
t[i + 1][0];
~~~~~~~~ ~^
我通过这样编译你的代码得到了:
g ++ -Wall main.cpp
标志墙可以发出所有警告,非常酷,让它成为你的朋友。
现在发生这种情况很有帮助,因为该行不会对您的代码产生任何影响。
PS:当然,我不知道你的代码中有check()
是什么,所以这就是我能说的。