iNe*_*ons -6 c++ initialization
所以,似乎'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)
mol*_*ilo 10
好主意!
这是一个打印"hellohello"五次的程序:
int i;
void print_twice(const std::string& s)
{
for (i = 0; i < 2; i++)
{
std::cout << s;
}
std::cout << std::endl;
}
int main()
{
for (i = 0; i < 5; i++)
{
print_twice("hello");
}
}
Run Code Online (Sandbox Code Playgroud)
或者......是吗?(不祥的风琴音乐播放.乌鸦在远处发出警报声.)
| 归档时间: |
|
| 查看次数: |
82 次 |
| 最近记录: |