chapter3-7.cpp

来自「C++STL程序员开发指南」· C++ 代码 · 共 36 行

CPP
36
字号
//文件名:CHAPTER3-7.cpp
#include <iostream.h>
#include <string.h>
// 定义函数模板,找出三个值中最小的值,与数据类型无关
template <class T>
T min(T ii, T jj, T kk)
{
T temp;
    if((ii<jj)&&(ii<kk)){		temp=ii;	}
	else if((jj<ii)&&(jj<kk)){		temp=jj;	}
	else	{		temp=kk;	}
	return temp;
}
//非模板函数重载
const char* min(const char* ch1, const char* ch2,const char* ch3)
{
	const char* temp;
    int result1 = strcmp(ch1,ch2);
    int result2 = strcmp(ch1,ch3);
    int result3 = strcmp(ch2,ch1);
    int result4 = strcmp(ch2,ch3);
	if((result1<0)&&(result2<0))	{		temp = ch1;	}
	else if((result3<0)&&(result4<0))	{		temp=ch2;	}
	else	{		temp=ch3;	}
	return temp;
}

// 下面是主函数
void main()
{
	cout<<min(100,20,30)<<endl;
	cout<<min(10.60,10.64,53.21)<<endl;
	cout<<min('c','a','C')<<endl;	
	cout<<min("Anderson","Washington","Smith")<<endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?