Vscode是常用的开发工具,代码插入能够把常见的代码嵌入智能提醒,减轻了很大的工作量,下面是我刷题的配置,直接复制黏贴到自己的cpp.json里就可以用了
左下角,打开设置,选择用户代码片段,选择自己的语言
{
// 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"
// }
"inc":{
"prefix": "inc",
"body":[
"#include <bits/stdc++.h>",
"",
"using namespace std;",
"",
"",
"",
"int main()",
"{",
"",
"",
" cout << a << endl;",
" return 0;",
"}",
],
"description": "头文件和主函数框架"
},
"输入长度为n的数组":{
"prefix": "nnum",
"body":[
"int n;",
"cin >> n;",
"vector<int> num;",
"for (int i = 0; i < n; ++i) {",
"int a;",
"cin >> a;",
"num.emplace_back(a);",
"}",
"Solution s;",
],
"description": "输入长度为n的数组"
},
"输入不定长数组":{
"prefix": "bnuminput",
"body":[
"vector<int> arr;",
"int n = 0;",
"while (cin >> n) {",
"arr.emplace_back(n);",
"if (cin.get() == '\n') break;// 遇到回车后停止输入",
"}",
"Solution s;",
],
"description": "输入不定长度的数组,回车停止输入"
},
"输入一行字符串":{
"prefix": "strinput",
"body":[
"string inputStr;",
"getline(cin, inputStr);",
"Solution s;",
],
"description": "输入一行字符串"
},
"for循环遍历":{
"prefix": "fori",
"body":[
"for (int i = 0; i < ${1:num.size()}; ++i) {",
" ",
"}",
],
"description": "for循环遍历"
},
"auto循环遍历":{
"prefix": "auto",
"body":[
"for (const auto& i : ) {",
" ",
"}",
],
"description": "auto循环遍历"
},
"自定义结构体Node":{
"prefix": "nodestruct",
"body":[
"typedef struct ${1:Node} {",
" ${2:int} ${3:a}; ",
" ${4:int} ${5:b};",
" $1() : $3(0), $5(0) {}",
" $1($2 $3, $4 $5) : $3($3), $5($5) {}",
"} $1;",
],
"description": "自定义结构体Node"
},
"自定义外部比较函数,sort要加()":{
"prefix": "cmp",
"body":[
"struct cmp {",
" bool operator()(const ${1:int}& a, const $1& b)",
" {",
" return",
" }",
"};",
],
"description": "自定义外部比较函数,sort要加()"
},
"leetcode核心代码模版":{
"prefix": "solu",
"body":[
"class Solution {",
" ${1:int} ${2:Func}(const int& a)",
" {",
" ",
" }",
"};",
],
"description": "leetcode核心代码模版"
}
}
占位符 $
$
后面紧跟数字可指定代码片段触发落入编辑器之后的光标位置,光标位置按照从小到大排,光标会默认落到$1
的位置,如果此时没有手动移动光标位置,按 TAB
则光标会落到 $2
的位置,$0
用于设置最终光标的位置,设置了 $0
之后,再往后设置其他占位符则不会生效, $0
终止了 TAB键
的光标跳转操作。
设置默认选项${1:default}
如果要设置占位内容的可选项,写法为 ${1|a,b,c|}
,括号中的 1
对应的是按 TAB
之后的光标落点顺序, abc
为可选的项,用逗号隔开。
如果想让代码缩进, 前边加个\t
。 同理\n
是换行