所以,似乎'i'几乎就是C++中的通用计数器.似乎在每个for循环中,人们重新初始化'i'.我不得不问,他们为什么不在全球范围内初始化'我'?'i'仍然需要在每个循环中重新定义,所以我不明白为什么会有任何混淆.
它看起来像这样:
#include <iostream>
int i=0;
int main()
{
for (i=0;i<3;i++)
{
std::cout << i << "\n";
}
for (i=0;i<5;i++)
{
std::cout << "hello" << "\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
比以下内容更容易阅读,编写速度更快:
#include <iostream>
int main()
{
for (int i=0;i<3;i++)
{
std::cout << i << "\n";
}
for (int i=0;i<5;i++)
{
std::cout << "hello" << "\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)