- 资料
比赛防保龄技巧之命名空间
- @ 2026-11-6 13:51:25
众所周知,Linux 系统下全局声明变量 yn,y1,将会喜提鸭蛋一颗。曾经也有选手英文水平太好,某道题有个跟管子有关,于是他写下了管子的英文pipe作为变量,结果0pts
那么怎么避免这种情况呢?
那就提到命名空间了
namespace HuiKuai{
...
int main(){
...
return 0;
}
}
int main(){
return HuiKuai :: main();
}
如果把代码放进自己的命名空间,变量的作用域就在这个命名空间内。
例如
namespace HuiKuai{
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int N = 1e6 + 9,inf = 1e9 + 9;
int y1 = 0;
void solve() {
cout << y1 << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(false);
// int t;cin>>t;while(t--)
solve();
return 0;
}
}
运行不会出错,因为程序先找到 HuiKuai::y1 而不是 <math> 里的全域 ::y1
值得注意的是命名空间对 #define 无效 ,这还得写外面
下面展示的我最近在用的模板
#include <bits/stdc++.h>
#define _for(i,a,b) for(int i = a,IM = b;i <= IM;i ++)
#define _rep(i,a,b) for(int i = a,IM = b;i >= IM;i --)
namespace HuiKuai{
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int N = 1e6 + 9,inf = 1e9 + 9;
void solve() {
}
int main() {
cin.tie(0)->sync_with_stdio(false);
// int t;cin>>t;while(t--)
solve();
return 0;
}
}
int main(){
return HuiKuai :: main();
}
1 条评论
-
sgapop LV 9 @ 2026-11-6 18:00:16
可恶的 linux 测评环境
- 1