重载和模板函数

#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

/*inline int max(int t1, int t2)
{
	return t1 > t2 ? t1 : t2;
}
inline float max(float t1, float t2)
{
	return t1 > t2 ? t1 : t2;
}
inline string max(string& t1, string& t2)
{
	return t1 > t2 ? t1 : t2;
}

inline int max(const vector<int> &vec)
{
	return *max_element(vec.begin(), vec.end());
}
inline float max(const vector<float> &vec)
{
	return *max_element(vec.begin(), vec.end());
}
inline string max(const vector<string> &vec)
{
	return *max_element(vec.begin(), vec.end());
}


inline int max(const int *parray, int size)
{
	return *max_element(parray, parray + size);
}
inline float max(const float *parray, int size)
{
	return *max_element(parray, parray + size);
}
inline string max(const string *parray, int size)
{
	return *max_element(parray, parray + size);
}*/

template <typename elemType>
inline elemType max(elemType &t1, elemType &t2)
{
	return t1 > t2 ? t1 : t2;
}
template <typename vectorType>
inline vectorType max(const vector<vectorType> &vec)
{
	return *max_element(vec.begin(), vec.end());
}
template <typename arraryType>
inline arraryType max(const arraryType *parray, int size)
{
	return *max_element(parray, parray + size);
}

void main()
{
	string sarray[] = { "we", "are", "pride", "of", "you" };
	vector<string> svec(sarray, sarray + 5);
	int iarray[] = { 12, 34, 2, 54, 222 };
	vector<int> ivec(iarray, iarray + 5);
	float farray[] = { 12.1, 34.2, 54.1, 23.5, 3.2 };
	vector<float> fvec = { farray, farray + 5 };
	
	string smax = max(max(svec), max(sarray, 5));
	int imax = max(max(ivec), max(iarray, 5));//括号也不能用中文输入法
	float fmax = max(max(fvec), max(farray, 5));
	

	cout << "smax should be were--found:" << smax << '\n';
	cout << max(svec) << endl;
	cout << max(sarray, 5) << endl;

	cout << "imax should be were--found:"  << imax << '\n';
	cout << max(iarray, 5) << endl;
	cout << max(ivec) << endl;

	cout << "fmax should be were--found:" << fmax << '\n';
	cout << max(farray, 5) << endl;
	cout << max(fvec) << endl;
	getchar();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值