Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

//**************************************

//     

//INCLUDE files for :maxn() -- finds lar

//     gest value in any array

//**************************************

//     

#include 

#include 

using namespace std;

 

code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

*

//     

/* maxn()

this function template (and explicit

specialization) will go through an

array of any generic type and find

the largest value in the array. if

there are two matching largest values,

the first one is returned.

The specialization is for an array

of char*. this returns the string with

the longest length in the array. Again,

if there are two strings in the array

with the same length, it will return

the first one (iow, the one with the

lower index).

BTW, if you wonder why I put a bunch of

// on blank lines, it's because I'm tryi

//     ng

to figure out a way to make my source 

appear as it should. Planetsourcecode seems

to have a way of always screwing up the

format of the source I post.

*LuckY*

lucky760@yahoo.com

*/

//

#include 

using namespace std;

#include 

//

/* Prototypes */

template 

T maxn(T*,int);

template <> char *maxn(char**,int);

//

    int main() {

     /* Some sample arrays */

     char *chr[10]={"One","Two","Three","Four","Five","Six","Seven",

     "Eight","Fifteen","Twenty"};

     double dbl[10] = {6000,3000,8000,10000,7000,5000,2000,4000,

     1000,9000};

    //

     /* Format the output */

     //note: newer compilers expect ios_base, not ios

     cout.setf(ios::fixed,ios::floatfield);

     cout.precision(0);

    //

     cout << "The largest values are: " << maxn(chr,10) <<

     " and " << maxn(dbl,10);

    //

     return 0; 

}

//

template 

    T maxn(T *list,int count) {

     int i=0,j=count-1;

     while (i

     list[i] >= list[j] ? --j : ++i;

     return list[i];

}

//

    template <> char *maxn(char **str,int count) {

     int i=0,j=count-1; 

     while (i

     strlen(str[i]) >= strlen(str[j]) ? --j : ++i; 

     return str[i]; 

}