Visual Studio 中配置初始化模板(#1,#2)
学习C++时,经常看到一些高手会用#1生成C模板、用#2生成C++模板,那是如何实现的呢?请往下看
利用#1 #2生成初始模板
-
打开Visual Studio 2019,在菜单栏选择Tools->Code Snippet Manager… -
在弹出的页面中,Language选择C++,在下面类似资源管理器的部分中选中Visual C++,记录下图中圈出的地址:  -
在上属地址对应的文件夹中创建 #1.snippet 和 #2.snippet 两个文件。具体如下: #1.snippet
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>#1</Title>
<Shortcut>#1</Shortcut>
<Description>c语言main函数</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>expression</ID>
<ToolTip>要计算的表达式</ToolTip>
<Default>true</Default>
</Literal>
</Declarations>
<Code Language="cpp"><![CDATA[#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
$selected$$end$
system("pause");
return EXIT_SUCCESS;
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
#2.snippet
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>#2</Title>
<Shortcut>#2</Shortcut>
<Description>c++语言main函数</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>expression</ID>
<ToolTip>要计算的表达式</ToolTip>
<Default>true</Default>
</Literal>
</Declarations>
<Code Language="cpp"><![CDATA[#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main()
{
$selected$$end$
system("pause");
return EXIT_SUCCESS;
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
- 新建工程进行测试:点击源文件——新建 源.c 和 源.cpp两个文件
在 源.c 文件 中 输入 #1 按下Tab,生成模板如下: 
在 源.cpp 文件 中 输入 #2 按下Tab,生成模板如下: 
|