VSCode 自定义代码片段 C++

VSCode 自定义代码片段

VSCode 可以很方便的自定义代码片段,一起来把自己常用的代码编成模板吧

操作过程:

  1. 依次点击 文件–首选项–用户代码片段
  2. 如果你想在所有工作区使用,选择新建全局代码片段
    如果你想限定作用域,选择新建文件夹的代码片段
    然后给片段文件起个名字,保存
    会有以下内容自动生成,这是使用说明,你可以选择全部删掉
{
	// Place your Cpp workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and 
	// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope 
	// is left empty or omitted, the snippet gets applied to all languages. 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": {
	// 	"scope": "javascript,typescript",
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
}

3.样例代码说明:
这是之前提到的重定向片段

{
	"Print to console": {
		"scope": "cpp",  //作用的语言,可多选,如"c,cpp,javascript,typescript",
		"prefix": "fre",  //触发器,可自定义
		"body": [
			"#ifdef LOCAL",
			"    freopen(\"E:/Cpp/1.in\", \"r\", stdin);",
			"    freopen(\"E:/Cpp/1.out\", \"w\", stdout);",
			"#endif",
			"    $0"  //回车后光标位置
		],
		"description": "Freopen"  //名称
	}
}

注:

  1. json输出"时要用\转义
  2. 双引号里面的空格都可以打印出来,最好算好空格数,出来排版效果好
  3. 触发器最好避开关键字与内置函数名字,长度最好三个字符以上

接下来就是发挥创造力的时候了

  1. cpp框架
{
	"Print to console": {
		"scope": "cpp",
		"prefix": "cpp",
		"body": [
			"#include <algorithm>",
			"#include <iostream>",
			"using namespace std;",
			"const int maxn = 1005;",
			"$1",
			"int main() {",
			"#ifdef LOCAL",
			"    freopen(\"E:/Cpp/1.in\", \"r\", stdin);",
			"    freopen(\"E:/Cpp/1.out\", \"w\", stdout);",
			"#endif",
			"",
			"",
			"",
			"",
			"",
			"",
			"    return 0;",
			"}",
			""
		],
		"description": "A cpp file template."
	}
}
  1. 有限状态自动机框架
{
	"Print to console": {
		"scope": "cpp",
		"prefix": "state",
		"body": [
			"#include <iostream>",
			"#define Error -1",
			"#define Start 0",
			"#define Accept 1",
			"#define Q1 2",
			"#define Q2 3",
			"#define Q3 4",
			"#define Q4 5",
			"",
			"int main() {",
			"#ifdef LOCAL",
			"    freopen(\"E:/Cpp/1.in\", \"r\", stdin);",
			"    freopen(\"E:/Cpp/1.out\", \"w\", stdout);",
			"#endif",
			"    int state = Start;",
			"    char ch;",
			"    $1",
			"    while (state != Accept && state != Error) {",
			"        switch (state) {",
			"        case Start:",
			"            break;",
			"        case Q1:",
			"            break;",
			"        case Q2:",
			"            break;",
			"        case Q3:",
			"            break;",
			"        case Q4:",
			"            break;",
			"            ",
			"        }",
			"    }",
			"    if (state == Accept)",
			"        printf();",
			"    else",
			"        printf();",
			"",
			"    return 0;",
			"}"
		],
		"description": "A template of state"
	}
}
  1. 这是个很好用的格式化输出模板(OJ经常要的每个输出那个空格隔开,但是最后一个后面不能有空格)三改的最简版本哦
{
	"Print to console": {
		"scope": "cpp",
		"prefix": "print",
		"body": [
			"for(int ${2:i} = 0; ${2:i} < ${1:n}; ${2:i}++)",
			"    printf(i ? \" %d\" : \"%d\", $3[i]);",
			"putchar('\\n');"
		],
		"description": "Print"
	}
}
  1. for循环
{
	"Print to console": {
		"scope": "cpp",
		"prefix": "fo",
		"body": [
			"for(int ${2:i} = 0; ${2:i} < ${1:n}; ${2:i}++)",
			"$3"
		],
		"description": "Loop"
	}
}
Visual Studio Code (VSCode) 是一个非常流行的代码编辑器,你可以通过扩展名为 `c_cpp_properties.json` 的文件来配置 C++ 项目的设置。以下是设置 C++ 代码片段的基本步骤: 1. **安装C/C++插件**: 首先,确保已安装了 "C/C++" 插件。如果你还没有安装,可以在 VSCode 市场(Extensions)中搜索并安装 "C/C++ Development Tools"。 2. **创建或打开`.c_cpp_properties.json`**: 打开或创建项目根目录下的 `.c_cpp_properties.json` 文件。如果文件不存在,可以右键点击项目名,选择 "Configure C/C++ Build System" 来自动生成。 3. **添加配置信息**: 在 JSON 格式中,你需要添加以下关键配置项: - `configurations`: 包含多个配置,针对不同的编译器(如 GCC、Clang),每个配置有一组特定的编译选项。 - `tasks`: 如果你想为编译和运行创建任务,这里配置对应的命令。 - `includePath`: 指定头文件路径,这样 VSCode 可以找到它们进行语法高亮和自动完成。 - `compilerPath`: 指定 C++ 编译器的路径。 例如: ```json { "configurations": [ { "name": "Win32", "includePath": ["$(workspaceFolder)/src", "${workspaceFolder}/external/include"], "defines": [], "compilerPath": "${env.GCC}", "cStandard": "c17", "cppStandard": "c++17", "intelliSenseMode": "gcc-x64" } ], "version": 4 } ``` 4. **保存并刷新**: 保存 `.c_cpp_properties.json` 后,重启或重新加载 VSCode 使新的设置生效。可以通过 `Ctrl + Shift + P` (Windows/Linux) 或 `Cmd + Shift + P` (Mac) 输入 "Reload Window" 来刷新。 5. **自定义代码片段**: 创建代码片段是在另一个文件夹内的 `.vscode` 中的 `snippets` 子目录下进行的。创建一个新的 `.json` 文件,例如 `c.snippets`,然后编写 C++ 语言的代码片段模板。例子如下: ```json { "scope": "source.c", "prefix": "cfunc", "body": [ "void ${1:functionName}(int ${2:params}) {", "\t${0}" "\treturn; // 示例代码终止符" "}" ], "description": "创建一个新的C++函数" } ``` 使用时,按下 `Ctrl + K, Ctrl + S` (Windows/Linux) 或 `Cmd + K, Ctrl + S` (Mac) 并输入 `cfunc` 来触发代码片段
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值