我听说我们应该尽可能地让变量变为本地变量,我同意.考虑以下代码:
int main() {
for(int i = 0; i<5; ++i) {
int temp;
std::cin >> temp;
std::cout << temp << std::endl;
}
return 0
}
Run Code Online (Sandbox Code Playgroud)
temp是for循环的局部变量.但是,我担心temp会在每个循环中声明,因此使程序运行得更慢.避免这种情况并temp在for循环外声明是否会更好?
int main() {
int temp;
for(int i = 0; i<5; ++i) {
std::cin >> temp;
std::cout << temp << std::endl;
}
return 0
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55 次 |
| 最近记录: |