快乐打码之 VS Code 配置代码模板

本文最后更新于:2021年4月27日 凌晨

# 快乐打码之 VS Code 配置代码模板(以C++为例)

依次点击:

文件(File)- 首选项(Preferences) - 用户代码片段(User Snippets)

image-20210421235419388

如图以创建cpp模板 cpp.json 为例

{
	"Print to console":{
		"prefix": "mycpp", // 预设的触发词,在新建页面中输入mycpp就会有智能提示,Tab或回车后会自动生成预设代码模板
		"body": [
			"#include <iostream>", // 常用头文件,可以自己修改
			"#include <cmath>",
			"#include <vector>",
			"#include <string>",
			"", // 空行
			"using namespace std;", // 标准命名空间
			"",
			"int main()", //main()函数
			"{",
			"    $0", //$0最终光标会在这里等待输入
			"    return 0;", //结束
			"}",
			"",
		],
		"description": "生成我的cpp模板代码" //用户输入后智能提示的内容
	}
}

也可以继续按照json格式写多几份模板,如搭配ACM的模板代码来使用,可以快速上手敲码。

{
	"Print to console -- cpp": {
		"prefix": "mycpp", // 预设的触发词,在新建页面中输入mycpp就会有智能提示,Tab或回车后会自动生成预设代码模板
		"body": [
			"#include <iostream>", // 常用头文件,可以自己修改
			"#include <cmath>",
			"#include <vector>",
			"#include <set>",
			"#include <map>",
			"#include <string>",
			"", // 空行
			"using namespace std;", // 标准命名空间
			"",
			"int main()", //main()函数
			"{",
			"    $0", //$0最终光标会在这里等待输入
			"    // system(\"pause\");", //标准C++的等待用户动作
			"    return 0;", //结束
			"}",
			"",
		],
		"description": "生成我的cpp模板代码" //用户输入后智能提示的内容
	},
	"Print to console -- ACM": {
		"prefix": "myacmcpp", // ACM比赛写题cpp模板
		"body": [
			"#include <bits/stdc++.h>", // only in GCC
			"#define IO\\ ",
			"    ios::sync_with_stdio(false);\\",
			"    cin.tie(0);\\",
			"    cout.tie(0);",
			"#define mem(a,x) memset(a,x,sizeof(a))",
			"#define per(x,a,b) for (int x = a; x <= b; x++)",
			"#define rep(x,a,b) for (int x = a; x >= b; x--)",
			"",
			"using namespace std;",
			"typedef long long LL;",
			"typedef pair<int, int> P;",
			"const int maxn = 1e5 + 10;",
			"const int mod = 1e9 + 7;",
			"const double eps = 1e-8;",
			"",
			"int main() {",
			"#ifdef LOCAL_RickyXu",
			"    freopen(\"test.in\",\"r\",stdin);",
			"    // freopen(\"test.out\",\"w\",stdout);",
			"#else",
			"    // IO;",
			"#endif // LOCAL_RickyXu",
			"    $0", //$0最终光标会在这里等待输入
			"    return 0;",
			"}",
			"",
		],
		"description": "生成我的ACM模板in C++代码"
	}
}

根据自己的实际情况来定制,效率upup!