vscode snippet 工程模板文件分享

2.3 自定义自己使用的代码片段

刷leetcode应该会很有用,比如引入一些链表node或者是图的定义等等。

比如写ROS的RVIZ输出的时候,能够有一个标准的代码片段的标准输出很有用。

比如写旋转矩阵或者类似的东西的时候

这里可以创建两种snippets的配置文件,第一种是global的全局snippets配置文件,适合一些注释等所有语言都适用的情况?

我主要是使用了第二种,针对某一种语言创建snippets配置文件

这里我主要为C++程序和Python程序创建了Code Snippet文件,我将我的代码文件template分享:

首先是Python的template模板(python.json文件)

Python 复制代码
{
	// Place your snippets for python 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": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
	"prefixs for leetcode": {
		"prefix": "prefix for leetcode",
		"body": [
			"from typing import List",
			"from queue import Queue",
		],
		"description": "prefix for leetcode"
	},
	"main for leetcode": {
		"prefix": "main for leetcode",
		"body": [
			"if __name__ == \"__main__\":",
			"    solution = Solution()",
			"    grid = [['1','1','0','0','0'],",
			"            ['1','1','0','0','0'],",
			"            ['0','0','1','0','0'],",
			"            ['0','0','0','1','1']]",
			"    result = solution.numIslands(grid)",
			"    print(f\"result = {result}\")",
			" ",
		],
		"description": "main for leetcode"
	},
	"bfs template": {
		"prefix": "bfs template",
		"body": [
			"def bfs(_grid: List[List[str]], _visited: List[List[bool]], _i: int, _j: int):",
			"    dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]]",
			"    que = Queue(0) # 0意味着初始化无线长度的queue",
			"    que.put([_i, _j])",
			"    _visited[_i][_j] = True",
			"    while not que.empty():",
			"        cur = que.get()",
			"        curx = cur[0]",
			"        cury = cur[1]",
			"        for n in range(4):",
			"            nextx = curx+dirs[n][0]",
			"            nexty = cury+dirs[n][1]",
			"            if(nextx < 0 or nextx >= len(_grid) or",
			"                nexty < 0 or nexty >= len(_grid[0])):",
			"                continue",
			"            if(_visited[nextx][nexty] is False and _grid[nextx][nexty] == '1'):",
			"                que.put([nextx, nexty])",
			"                _visited[nextx][nexty] = True",
		],
		"description": "bfs template"
	}

}

其次是C++的template模板(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": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
	"sort algorithm with lambda": {
		"prefix": "sort_lambda",
		"body": [
			"std::vector<int> v;",
			"\/\/ 需要对v从大到小排列",
			"sort(v.begin(), v.end(),[](int a, int b){return a>b;});"
		],
		"description": "Log output to console"
	},
	"prefixs for leetcode": {
		"prefix": "prefix for leetcode",
		"body": [
			"#include <algorithm>",
			"#include <iostream>",
			"#include <unordered_set>",
			"#include <vector>",
			"#include <math.h>",
			"#include <limits.h>",
			"using namespace std;",
			"struct TreeNode {",
			"   int val;",
			"   TreeNode *left;",
			"   TreeNode *right;",
			"   TreeNode() : val(0), left(nullptr), right(nullptr) {}",
			"   TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}",
			"   TreeNode(int x, TreeNode *left, TreeNode *right)",
			"       : val(x), left(left), right(right) {}",
			"};",
		],
		"description": "prefix for leetcode"
	},
	"main for leetcode": {
		"prefix": "main for leetcode",
		"body": [
			"int main() {",
			"   // 示例二叉树",
			"   Solution solution;",
			"   vector<int> nums = {7, 1, 5, 3, 6, 4};",
			"   int result = solution.maxProfit(nums);",
			"   std::cout << \"result: \" << result << std::endl;",
			"}",
		],
		"description": "main for leetcode"
	}
}
相关推荐
Jackson@ML5 小时前
2026最新版Visual Studio安装使用指南
ide·c#·visual studio
虚神界熊孩儿6 小时前
OpenStation + VSCode :本地大模型赋能编码效率的实战指南
vscode·大模型部署·大模型本地部署
chao_6666667 小时前
Claude Code for vscode 新手入门完整教程
ide·vscode·ai·编辑器·ai编程·claude
Satellite_H7 小时前
Keil + VSCode 优化开发体验
ide·vscode·编辑器
v_for_van9 小时前
STM32低频函数信号发生器(四通道纯软件生成)
驱动开发·vscode·stm32·单片机·嵌入式硬件·mcu·硬件工程
lingzhilab9 小时前
零知IDE—— ESP8266(ESP-12F)MESH 组网实现多设备智能家居控制系统(灯光 / 传感器 / 人体感应)
c++·ide·智能家居
HAPPY酷11 小时前
Visual Studio 原生项目(.vcxproj) 和 CMake 项目对比
ide·visual studio
__xu_11 小时前
【总结】查看某个文件git提交记录的两种方法
git·vscode·提交记录
柠檬叶子C12 小时前
STM32CubeIDE 安装教程 | 2026最新STM32CubeIDE安装教程 | STM32CubeIDE保姆级安装教程
ide·stm32·嵌入式硬件
山有木兮啊12 小时前
VSCode Remote-SSH 连接Mac卡在初始化VSCode
vscode·macos·ssh