Array of pointer-to-char loop.

Greetings,

My question is about the specialisation at the very bottom.
I am trying to take an array of pointers-to-char and the number of pointers as arguments and then, using a loop, determine which string is the longest (if there's a tie, the string that's located earlier in the array wins).
What am I doing wrong? The method I used works with the template function when I apply it to a regular array of ints/doubles.

Thanks for your help.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

Write a template function maxn() that takes as its arguments an array of items of
type T and an integer representing the number of elements in the array and that
returns the largest item in the array. Test it in a program that uses the function
template with an array of six int values and an array of four double values.

The program should also include a specialisation that takes an array of pointer-to-char as
an argument and the number of pointers as a second argument and that returns address
of the longest string. If multiple strings are tied for having the longest length,
the function should return the address of the first one tied for longest.
Test the specialisation with an array of five string pointers.
*/

#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;


template <typename T>
T maxn(T *x, int n);

template <> const char * maxn(const char * pc[], int n);

int main()
{
	const int inum = 6;
	const int dnum = 4;
	int intar[inum] = { 1, 2, 3, 12, 5, 6 };
	double doublar[dnum]{ 1.0, 6.45, 3.0, 4.0 };
	cout << "The biggest value of intar: " << maxn(intar, inum) << endl;
	cout << "The biggest value of doublar: " << maxn(doublar, dnum) << endl;

	const int pcnum = 5;
	const char * pc[pcnum] =
	{
		"One", "This is two", "And this is three", "And this definitely 
                 must be four", " This is five"
	};
	cout << "The longest string is: " << maxn(pc, pcnum) << endl;

	system("pause");
	return 0;
}

template <typename T>
T maxn(T *x, int n)
{
	T max = 0;
	for (int i = 0; i < n; i++)
	{
		max = x[i] > max ? x[i] : max;
	}

	return max;
}

template <> const char * maxn(const char * pc[], int n)
{
	int max = 0;

	for (int i = 0; i < (n - 1); i++)
	{
		max = strlen(pc[i]) >= strlen(pc[i + 1]) ? i : max;
	}


	return pc[max];
}
> What am I doing wrong?
¿what error are you having?

> The method I used works with the template function
- no, it doesn't
- the algorithm is different
@ne555
¿what error are you having? - No error. The code compiles but I get the pointer to the last string ("This is five") returned instead of the longest one. By ,,What am I doing wrong?" I meant the fact that the function doesn't return the "address of the longest string. If multiple strings are tied for having the longest length, the function should return the address of the first one tied for longest."
> but I get the pointer to the last string ("This is five") returned
weird, I've got the correct answer and don't see any invalid access.

your comparison is incorrect, it should be strlen(pc[max]) < strlen(pc[i])


> The method I used works with the template function
consider an array full of negative numbers, you return 0 as maximum.
Last edited on
Thank you for your help. :)
Topic archived. No new replies allowed.