vscode 自定义代码模板
-
select User snippets from Settings on the bottom left corner.
-
select a certain language
for example: cpp
- create your own snippets
格式如下,防着写
- 第一行"cpp template",模板的名称
- “prefix”,快捷键,在文件中输入cpp,按下tab就会自动填充"body"里的代码。
- ${CURRENT_YEAR} 这个是个变量,当年年份
- $1, $2,$3…是光标的顺序,按TAB键就可以切换为下一个,$0特殊 为光标最后所在地方。
- "description"代码片段描述,随便写写。
{
"cpp template": {
"prefix": "cpp",
"body": [
"/** ",
" * @Date: ${CURRENT_YEAR} ${CURRENT_MONTH_NAME_SHORT} ${CURRENT_DATE}",
" * @Time: ${CURRENT_HOUR}:${CURRENT_MINUTE}",
" * @Author: Chris",
" * @Desc: ${1:Input the description of this file}",
"**/"
"#include <iostream>",
"",
"using namespace std;",
"",
"int main() {",
" $0",
" return 0;",
"}",
],
"description": "A cpp file template."
}
}
- Usage
输入cpp,然后按下tab
效果如图,光标先跳到desc 这里输入完文件描述,然后按下tab,光标就会跳到主函数里面
Reference
Snippets in Visual Studio Code
Javascript
二维数组初始化
let res = new Array(n).fill(0).map(
() => new Array(n).fill(0)
)
为什么要添加fill(0),而不是直接调用map()?
- The map() method in JavaScript is used to create a new array populated with the results of calling a provided function on every element in the original array. This method is an iterative method, meaning it calls the provided callbackFn function once for each element in the array, constructing a new array from these results. Crucially, callbackFn is invoked only for array indices that have
assigned values
; it is not invoked for empty slots in sparse arrays