使用函数重载实现整数,字符,字符串大小比较
c++使用函数重载实现整数,字符,字符串大小比较 不知道你想说什么 想要源码的话你不如直接百度,论坛是代码查错的 想知道一下字符串大小的比较 横刀上京1729 发表于 2020-9-19 18:02
想知道一下字符串大小的比较
? 本帖最后由 xieglt 于 2020-9-19 23:44 编辑
#include "string.h"
typedef struct tagCharPoint
{
char * p;
struct tagCharPoint(char * str)
{
p = str;
}
operator > (struct tagCharPoint & cp)
{
return strcmp(p,cp.p) > 0;
}
operator < (struct tagCharPoint & cp)
{
return strcmp(p,cp.p) < 0;
}
}CHARPOINT;
template<typename T>long commCompare(T t1,T t2)
{
long ret = 0;
if(t1 > t2)
{
ret = 1;
}
else if(t1 < t2)
{
ret = -1;
}
return ret;
}
int main()
{
int a,b;
char c,d;
long e,f;
float g,h;
double i,j;
a = 1000;
b = 2000;
c = 'c';
d = 'd';
e = 3000;
f = 2500;
g = 158.3f;
h = 132.1f;
i = 12345.5;
j = 5432.1;
long ret = commCompare(a,b);
ret = commCompare(c,d);
ret = commCompare(e,f);
ret = commCompare(g,h);
ret = commCompare(i,j);
char * str1 = "Hello,world";
char * str2 = "good luck";
CHARPOINT cp1(str1),cp2(str2);
ret = commCompare(cp1,cp2);
return 0;
}
页:
[1]