VSCode 小技巧:创建 OI 的 C++ 代码模板

为了快速书写 C++ 程序,其实还是为了偷懒,我们可以使用 VSCode 的 Template 功能,创建一个 c++ 模板代码。

建立 C++ 模板

第一步

"File" -> "Preference" -> "User snippets"。将出现如下图所示的情况。

第二步

选择了 User Snippets 后,在输入框中输入 c++。如下图,VSCode 就会创建一个 cpp.json 文件。

第三步

将在 cpp.json 中写入自己的 cpp 文件模板,并保存即可。我设置的文件模板如下。

{
	// Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
	// same ids are connected.
	// Example:
	"Print to console": {
		"prefix": "c++",
		"body": [
			"#include <bits/stdc++.h>",
			"",
			"using namespace std;",
			"",
			"int main() {",
			"	return 0;",
			"}"
		],
		"description": "A cpp file template"
	}
}

这里需要特别注意的是 prefix 设置,我设置为 c++,这个设置的意思是,当你在某个 .cpp 文件中输入 c++ 这个字后, vscode 就会认为你需要插入一个模板(template)。

使用样例

第一步

新建并保存一个 cpp 文件。如下图所示。

这里,我们新建并保存了一个 cpp 文件,名字是 123.cpp。

第二步

在 cpp 中输入 c++,VSCode 的解析将如下图。

如上图,VSCode 解析出 c++ 是我们刚才新建的 cpp 模板,注意 cpp.json 中的 prefix 和 description。

这样,我们直接回车,VSCode 就会将 cpp.json 中定义的模板内容插入。如下图所示。

果然懒惰是第一生产力。

猜你喜欢

转载自blog.csdn.net/justidle/article/details/108811152
今日推荐