Visual Studio如何快捷地导入代码块

在C++项目中,经常需要写一些头文件,冒泡排序代码,循环语句,使用Visual Studio如何快捷地导入代码块,比如下面这种形式。
Visual Studio如何快捷地导入代码块_第1张图片
首先,创建一个文件夹存放,存在以.snippet文件,注意文件的命名中需要加#
Visual Studio如何快捷地导入代码块_第2张图片
打开Visual Studio —》工具—》代码段管理器—》选择语言:Visual C+±–》添加导入
Visual Studio如何快捷地导入代码块_第3张图片
代码块其实就是VS所要识别的XM格式
以下是导入头文件等代码块

<?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>具有头文件的代码片段</Description>
			<Author>Microsoft Corporation</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
				<SnippetType>SurroundsWith</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>name</ID>
					<ToolTip>类名</ToolTip>
					<Default>MyClass</Default>
				</Literal>
			</Declarations>
			<Code Language="cpp"><![CDATA[#define _CRT_SECURE_NO_WARNINGS
				#include <stdio.h>
				#include <string.h>
				#include <stdlib.h>
				#include <math.h>
				#include <time.h>
				#include <Windows.h>

				int main(void)
				{

					system("pause");
					return EXIT_SUCCESS;
				}
			]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

Visual Studio如何快捷地导入代码块_第4张图片
附上导入C++格式代码块

<?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++代码块</Description>
			<Author>Microsoft Corporation</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
				<SnippetType>SurroundsWith</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>name</ID>
					<ToolTip>类名</ToolTip>
					<Default>MyClass</Default>
				</Literal>
			</Declarations>
			<Code Language="cpp"><![CDATA[#define _CRT_SECURE_NO_WARNING
				#include <iostream>
				using namespace std;
				
				int main()
				{
					$selected$$end$
					
					system("pause");
					return EXIT_SUCCESS;
				}
			]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

冒泡排序代码块

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>#3</Title>
			<Shortcut>#3</Shortcut>
			<Description>冒泡排序</Description>
			<Author>Microsoft Corporation</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
				<SnippetType>SurroundsWith</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>name</ID>
					<ToolTip>类名</ToolTip>
					<Default>MyClass</Default>
				</Literal>
			</Declarations>
			<Code Language="cpp"><![CDATA[
void BubbleSort(int * src, int len)
{
	for(int i = 0; i < len - 1; i++)
	{
		for(int j = 0; j < len - 1 -i; j++)
		{
			if(src[j] > src[j+1])
			{
				int temp = src[j];
				src[j] = src[j+1];
				src[j+1] = temp;
			}
		}
	}
}
			]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

你可能感兴趣的:(C++,编程)