ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hello, i am working on programming tasks form book but i am stuck at one task where i don't clearly understand what to do and have no idea how to do it. i've done only part of the problem.
here's question(sorry for my engish, it's not my native lang.):
Code:
This part i've done:
write template of function maxn() that takes two arguments,
first is array of type T
second is an integer representing number of items in the array.
the function returns its biggest value.
Use the template in the program that uses the function template with array of six ints and four doubles.
At this point i am stuck:
the program should contain specialization that takes two args.
first: array of pointers to char
second: number of pointers.
it should return address of the longest string.
test the specialization with array of five strings pointers
here's my code:
Code:
#include <iostream>
using namespace std;
template <typename T>
T maxn(T arr[], int n);
void maxn<char>(char * arr[], int n);
int main()
{
int integers[6] = {1,3,6,4,0,2};
double dble[4] = {193.69, 207.02, 0.09, 20.1};
cout << "Max value from int is: ";
cout << maxn(integers, 6) << endl;
cout << "Max value from double is: ";
cout << maxn(dble, 4);
}
template <typename T>
T maxn(T arr[], int n)
{
T max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
the template has comparison operator, so arr[i] > max will call the comparator of the specific type. Using strings the comparator is working based on the alphabetical order. You need the longest one, so you need to modify how this comparator should work.
i know how to figure out which string is the longest one but i don't know what the declaration should look like. can you show me one ?
you can see i've tried one but i am not sure if this is correct:
...
int main()
{
int integers[6] = {1,3,6,4,0,2};
double dble[4] = {193.69, 207.02, 0.09, 20.1};
string strings[5] = { "asdf", "qwer", "retyhrw", "3q4trc", "34rcq" };
cout << "Max value from int is: ";
cout << maxn(integers, 6) << endl;
cout << "Max value from double is: ";
cout << maxn(dble, 4) << endl;
cout << "Max value from strings is: ";
cout << maxn(strings, 5) << endl;
}
this code works well with strings using the same maxn, just it will return the last one not the longest one.
The problem is the comparison operator, you need to override it.
So create a new class: mystring:
Yes, that is a template specialization and I've even seen compilers accept that. But if I understand correctly, that is not valid C++. I think the standard says you can't fully specialize a templated function.
Quote:
Alternatively, you can try
Code:
void maxn(char * arr[], int n);
That's a non-template function. Non template functions are preferred to templates of the same name.
That is overloading the function, which so far as I understand is the only correct approach to this situation.
#include <iostream>
#include <string.h>
using namespace std;
template <typename T> T maxn(T arr[], int n);
template <> char * maxn(char * arr[], int n);
int main()
{
int integers[6] = {1,3,6,4,0,2};
double dble[4] = {193.69, 207.02, 0.09, 20.1};
string strings[5] = { "asdfrtyurty", "qwer", "retyhrw", "3q4trc", "34rcq" };
char * chars[5] = { "asdfrtyurty", "qwer", "retyhrw", "3q4trc", "34rcq" };
cout << "Max value from int is: ";
cout << maxn(integers, 6) << endl;
cout << "Max value from double is: ";
cout << maxn(dble, 4) << endl;
cout << "Max value from strings is: ";
cout << maxn(strings, 5) << endl;
cout << "Max value from chars is: ";
cout << maxn(chars, 5) << endl;
}
template <typename T>
T maxn(T arr[], int n)
{
T max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
template <>
char * maxn<char *>(char *arr[], int n)
{
unsigned max = strlen(arr[0]);
char * r = arr[0];
for (int i = 1; i < n; i++) {
if (strlen(arr[i]) > max) {
max = strlen(arr[i]);
r = arr[i];
}
}
return r;
}
_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
Yes, that is a template specialization and I've even seen compilers accept that. But if I understand correctly, that is not valid C++. I think the standard says you can't fully specialize a templated function.
It compiles with g++ -Wall -Wextra -pedantic -ansi without errors. I do not have a copy of the standard (Is one available somewhere? Probably not.), so I can't tell for sure what the standard says about it. I never really used this, actually. But I have seen this used (I believe even in some tutorials). That does not mean it's correct, of course
Actually, isn't it the other way around: function templates can be fully specialized but notpartially specialized?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.