resolve which one is the biggest string

I dint know that I normal comparation with string, what It does is get the first letter of both string compared and the one is before in the alphabet is the bigger one, I would like to get the one has more letter...that resolution is inside of a bigger code made to resolve with any kind of type...

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
 // class templates
#include <iostream>
using namespace std;

template<class T>
class myclass{
	T a,b;
	
public:
	
	myclass (T x,T y):a(x),b(y){}; 
	
	T resolve( ){
		T bigger;
		bigger =  this->a > this->b ? this->a:this->b;
		cout<<bigger<<"\n";
		return bigger;
	}
};

int main(){

	myclass<int> myobjectint(30,25);
	myclass<float> myobjectfloat(30.23,30.56);
	myclass<string> myobjectstring("malaga","granada");
	myobjectint.resolve();
	myobjectfloat.resolve();
	myobjectstring.resolve();
	return 0;
}


the output for string is malaga when granada has more letters...


Thanks!!
I would like to get the one has more letter...

Have you thought about using string::length??

http://www.cplusplus.com/reference/string/string/length/

Andy
Thanks I have done this....but I have had to define other member function as It can not be the same than with teh rest....do you think I could use the same function?do I have to use a Template specialization for that?..

This has been my function with length().

1
2
3
4
5
6
7
8

	T resolvestring(){
		T bigger = this->a.length() > this->b.length() ? a:b;
		cout<<bigger<<"\n";;
		return bigger;
		
	}
Yes.. you can use Template specialization for this ..

...that said, I think you should be specializing the T and not the myclass. Let your class's users decide what 'bigger' means.
when you say "class's users " you mead , the member function of my class,dont you??
No, the user of a class is the code that makes use of it.

It is true that a class can technically make use of itself, such use doesn't count.

So for your code, you'll need a 'bigger means longer string class' wrapper around std::string, so that you can have:

myclass <mystringwrapper> myobjectstring(...);

Hope this helps.
Or you can ask for an optional parameter, a comparison class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class compare_length{
public:
   bool operator()(const std::string &a, const std::string &b){
      return a.length() < b.length();
   }
};

template<class T, class Comp = std::less<T> >
class myclass{
	T resolve( ){
		T bigger;
		bigger =  Comp()(this->b, this->a) ? this->a: this->b;
		cout<<bigger<<"\n";
		return bigger;
	}
};
myclass< std::string, compare_length > foo(...); //use length to compare
myclass< std::string> bar(...); //lexicographic comparison 
Topic archived. No new replies allowed.