C++:竞赛编程中缩短代码的方法?
在本部分中,我们将看到一些适合竞赛编程的代码缩减策略示例。假设我们必须编写大量代码。在该代码中,我们可以遵循一些策略来使其更短。
我们可以更改类型名称使其更短。请检查以获取该思路的代码
示例代码
#include <iostream>
using namespace std;
int main() {
long long x = 10;
long long y = 50;
cout << x << ", " << y;
}输出
10, 50
示例代码(使用 typedef 缩短)
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
ll x = 10;
ll y = 50;
cout << x << ", " << y;
}输出
10, 50
如此一来,我们无需一次次写下“long long”,而可以使用“ll”。
使用 typedef 的另一个示例如下。编写模板或 STL 函数时,我们也可以使用宏来缩短代码。我们可以如下使用它们。
示例
#include <iostream>
#include <vector>
#define F first
#define S second
#define PB push_back
using namespace std;
typedef long long ll;
typedef vector<int< vi;
typedef pair<int, int< pii;
int main() {
vi v;
pii p(50, 60);
v.PB(10);
v.PB(20);
v.PB(30);
for(int i = 0; i<v.size(); i++)
cout << v[i] << " ";
cout << endl;
cout << "First : " << p.F;
cout << "\nSecond: " << p.S;
}输出
10 20 30 First : 50 Second: 60
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP