模板函数及一些代码规范
2016-02-24 13:48
369 查看
使用Visual Studio 2012进行小程序编写过程,养成一定的代码规范,用于从学校到职场的过渡……
![](https://img-blog.csdn.net/20160224133641783)
创建空项目,填写项目名。
![](https://img-blog.csdn.net/20160224133747517)
添加 .cpp文件或 .h头文件
![](https://img-blog.csdn.net/20160224133841080)
更改文件名,使用英文单词,令其功能一目了然
代码编写规范
运行结果
![](https://img-blog.csdn.net/20160224134029108)
//使用模板函数可以创建创建适用不同参数类型,但功能相同的功能函数。
//使用引用参数能避免复制值给形参,减少操作。交换函数若使用传值参数,会造成无法对实参进行值交换。
//用关键字const来指明函数不可修改的引用参数
创建空项目,填写项目名。
添加 .cpp文件或 .h头文件
更改文件名,使用英文单词,令其功能一目了然
代码编写规范
/********************************************************** *Name: Ctemplate.cpp *Content: Used for demonstrating template function *Instructions: none *Version: V1.0 *Author: Caddress *Data: 20160224 ***********************************************************/ #include<stdio.h> #include<iostream> using namespace std; //declarations of swap() template<class Ta, class Tb> void swap(Ta& a, Tb& b); //declarations of abc() template<class Ta, class Tb, class Tc> Ta abc(Ta&a, Tb&b, Tc&c); /********************************************************** *Function: main *Input: none *Output: string of results *Return: void *Data Version Author Content *---------------------------------------------------------- *20160224 V1.0 Caddress create ***********************************************************/ void main() { int x = 3, y = 4, z = 5; swap(x, y); cout <<"The results of the exchange is x = "<< x <<" y = "<< y <<";"<< endl; cout <<"The results of the calculation is "<<abc(x,y,z) <<";"<<endl; //Press any button to exit getchar(); } /********************************************************** *Function: swap *Input: a , b *Output: none *Return: void *Data Version Author Content *---------------------------------------------------------- *20160224 V1.0 Caddress create ***********************************************************/ template<class Ta, class Tb> void swap(Ta& a, Tb& b) { int temp = a; a = b; b = temp; } /********************************************************** *Function: abc *Input: a , b , c *Output: none *Return: a + b * c *Data Version Author Content *---------------------------------------------------------- *20160224 V1.0 Caddress create ***********************************************************/ template<class Ta, class Tb, class Tc> Ta abc(Ta&a, Tb&b, Tc&c) { return a + b * c; }
运行结果
//使用模板函数可以创建创建适用不同参数类型,但功能相同的功能函数。
//使用引用参数能避免复制值给形参,减少操作。交换函数若使用传值参数,会造成无法对实参进行值交换。
//用关键字const来指明函数不可修改的引用参数
相关文章推荐
- SourceProvider.getJniDirectories
- 执行失败的任务 ': processDebugManifest'
- 如何安装SQL Server 2008 Management Studio Express
- 获取免费的Camtasia Studio 2使用授权
- SQLServer 2012中设置AlwaysOn解决网络抖动导致的提交延迟问题
- 如何使用Visual Studio 2010在数据库中生成随机测试数据
- Visual Style中的shellstyle.dll文件修改方法
- Chrome Visual Studio 2005下的编译过程
- MSN Messenger 化繁为简(用 Visual Studio 2005 去掉 MSN 广告)
- Android Studio使用教程(六):Gradle多渠道打包
- Visual C++中Tab View的多种实现方法
- zend studio 快捷键收集
- SQL Server 2012 创建定时作业(图文并茂,教你轻松快速创建)
- Visual Studio快速开发以及Visual Studio 2010新功能介绍
- C#中几个未知的Visual Studio编码技巧分享
- Windows 2012 R2 Configuration Scripts
- Android Studio快速创建常用工具类的插件Utils
- AAPT err(Facade for): libpng error: Not a PNG file 错误
- Android Studio 性能优化常用工具
- android提示:Gradle DSL method not found: 'android()'