论C++的错误写法
White_Falcon · · 题解
雷迪生俺的杰特曼
通过我长久以来的C++经验
给大家总结了一下与C++相关的知识
首先是
赋值与变量或常量及数组的命名
1.开了万能头之后必须给变量取名为
abc
abs
max
min
cin
cout
printf
scanf
y1
auto
_gcd
等一系列的 -错误命名- 好名称
你若是题目做不出来就绝对是变量名的问题
2.常量的命名规则
2.1.首先我们的常量名也是可以取上述变量的名字的
2.2. 还有就是常量取好之后一定要运算其值,而产生变化,例如这段代码:
#include<bits/stdc(++(cao)).h>
using namespace std;
int main(){
const int N=1;
N--;
cout<<N++;
return 1;
}
这无疑是一片相当完美的代码,以后我们都得这样写
3.数组的定义
上面我们都已经学完常量和变量了,数组该如何定义呢???
3.1.数组一定使用变量命名,否则就会错误 例如
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[3];
cin>>a[0]>>a[1]>>a[2];
return 0;
}
啊!!!多么
我们一定不要写成这样
还有这个
#include<bits/stdc++.h>
using namespace std;
int main(){
const int N=1e5+5;
int n;cin>>n;
int a[N];
for(int i=0;i<n;i++){
cin>>a[i];
}
return 0;
}
啊!!真的看不下去了,这都什么
来让我们看正确的写法吧
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(const int i=0;i<n;i=i+1-1+1-1+1){
cin>>a[i];
}
return 0;
}
啊!!!多莫完美的代码啊!!!
这种代码真的tai香了