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

本文介绍如何在VSCode中配置C++模板,通过简单步骤实现快速创建标准C++程序结构,提高编码效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了快速书写 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 中定义的模板内容插入。如下图所示。

果然懒惰是第一生产力。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值