函数模板的好处

本文展示了一个使用C++模板函数来显示各种数据类型最小值和最大值的示例代码,包括short、int、size_t、float、double和long long等类型。通过使用STL中的numeric_limits类,此代码能够有效地获取并打印出这些类型的数值范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once


#define WIN32_LEAN_AND_MEAN        // 从 Windows 头中排除极少使用的资料
#include 
<stdio.h>
#include 
<tchar.h>



// TODO: 在此处引用程序需要的其他头文件
#include <iostream>
#include 
<limits>

 

 

#include "stdafx.h"
using std::cout;
using std::endl;
using namespace std;

template
<class T>
void showMinMax(){
    cout
<<"Min: "<<numeric_limits<T>::min()<<endl;
    cout
<<"Max: "<<numeric_limits<T>::max()<<endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
    showMinMax
<short>();
    showMinMax
<int>();
    showMinMax
<size_t>();
    showMinMax
<float>();
    showMinMax
<double>();
    showMinMax
<long long>();
    
return 0;
}


 

转载于:https://www.cnblogs.com/NeuqUstcIim/archive/2008/12/23/1360334.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值