A question about the function templates and function pointers

Although I have not this question ...... AC
2018 midterm process set A07

Enter
the first line is an integer of n, there are n sets of data represents
each data line 2 has
a first row is an integer of 10
second row is four strings without spaces, separated by a space therebetween
outputted
to the output 10 the third to seventh inside the square of an integer and
re-output 10 in integer from 3 through 7, in the manner of the string, the result serially connected
and then outputs four strings, the first one to fourth string, all characters in the ASCII code plus an integer obtained and
after re-output four strings, the 1st to 4th string, copy it again respectively, in the manner of the string, connected in sequence results.
Sample input
. 1
. 1. 5. 4. 3 2. 6. 7. 9 10. 8
Machine, Learning!
Sample output
135
34567
1586
MachineMachine, LearningLearning !!

Fill in the blank program.

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
// 在此处补充你的代码
string int2string(int x) { return to_string(x); }
int int2squareint(int x) { return x * x; }

int string2int(string str) {
	int res = 0;
	for (string::iterator iter = str.begin(); iter != str.end(); ++iter)
		res += *iter;
	return res;
}
string string2longerstring(string str) { return str + str; }

int main() {

	int t;
	cin >> t;
	while (t--) {
		int b1[10];
		for (int i = 0; i < 10; ++i)

			cin >> b1[i];
		A<int, 10> a1 = b1;
		cout << a1.sum(2, 6, int2squareint) << endl;
		cout << a1.sum(2, 6, int2string) << endl;

		string b2[4];
		for (int i = 0; i < 4; ++i)
			cin >> b2[i];

		A<string, 4> a2 = b2;
		cout << a2.sum(0, 3, string2int) << endl;
		cout << a2.sum(0, 3, string2longerstring) << endl;
	}
	return 0;
}

The key is to write a function that sum. I compiled the delay could not pass.

template <class T2>
		T2 sum(int b, int e, T2 func) {
			static T2 s;
			for (int i = b; i <= e; i++) {
				s = s + func(*(pointer + i));
			}
			return s;
		}

This is the beginning of a write function. Note that the type is not a function declaration time T2 such as string or int, a string * (int) in the example. Oh, if the T x T, then write directly to the local run is also no problem. Then s T2 in front of T3 but can not change over to remind ourselves ...... Do not make such a mistake mentally.
Refer to someone else's code, so if you want to determine the value of a local variable as a function of the type, then return to re-write template function pointer, not directly a T in the past. Last answer:

class A{
	public:
		T *pointer;
		A(T* p): pointer(p) {}
		template <class T2>
		T2& sum(int beginpos, int endpos, T2 (*func)(T x)) {
			static T2 s ;
			for (int i = beginpos; i <= endpos; i++) {
				s = s + func(*(pointer + i));
			}
			return s;
		}
};

I guess the question should be initialized s led WA, can not think of any other reason. But if I add direct s = 0 will appear runtime error, tired heart ...... temporarily not think there is any solution.

Guess you like

Origin blog.csdn.net/weixin_44288817/article/details/89842085