Function templates help!!

I need to write a function templates that can find 3 int,float,char,and strings long but when I use strlen or sizeof try to find the long but it doesnot work it just print out the second word
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  #include<iostream>
#include<string>
using namespace std;

template <typename T>
T getMax(T v1,T v2,T v3)
{
	if(v1>v2 && v1>v3)
	{
		return v1;
	}
	else if(v2>v3)
	{
		return v2;
	}
	else
	{
		return v3;
	}
	
}
int main()
{
	int a,b,c;
	float a1,b1,c1;
	char a2,b2,c2;
	string a3="Yi",b3="yile",c3="hello";
	cout<<"Enter 3 value: \n";
	cin>>a>>b>>c;
	cout<<"Enter 3 floats:\n";
	cin>>a1>>b1>>c1;
	cout<<"Enter 3 chars:\n";
	cin>>a2>>b2>>c2;
	cout<<"Enter 3 strings:\n";
	cout<<"The biggest value is: "<<getMax(a,b,c)<<endl;
	cout<<"The biggest floats is: "<<getMax(a1,b1,c1)<<endl;
	cout<<"The biggest chars is: "<<getMax(a2,b2,c2)<<endl;
	sizeof a3;
	sizeof b3;
	sizeof c3;
	cout<<"The biggest chars is: "<<getMax(a3,b3,c3)<<endl;
	system("pause");
}
I'm not sure I understand your problem. "yile" is considered the biggest string value because the comparison is using the lexicographical order. Simply put you can say that "yile" is biggest because 'y' has a bigger ASCII code than 'Y' and 'h'.

http://en.cppreference.com/w/cpp/string/basic_string/operator_cmp
http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare

If you want the strings to be compared in some other way you might want to specialize the function template for the string type.
Topic archived. No new replies allowed.