Šablony funkcí II

{“https:\/\/www.jdoodle.com\/a\/afO?format=text”:”#include <iostream>\nusing namespace std;\n\ntemplate<int SIZE, typename T>\nT soucet(T* array) {\n T sum = 0;\n for(int i = 0; i < SIZE; i++) {\n sum += array[i];\n }\n return sum;\n}\n\nconst int MAX_SIZE = 10;\ndouble pole[MAX_SIZE] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1 };\n\nint main() {\n cout << "Soucet je " << soucet<MAX_SIZE>(pole) << endl;\n return 0;\n}”,”https:\/\/www.jdoodle.com\/a\/ag2?format=text”:”#include <iostream>\nusing namespace std;\n\n\/\/ S\u010d\u00edt\u00e1 jednorozm\u011brn\u00e9 pole\ntemplate<int SIZE, typename T>\nT soucet(T* array) {\n T sum = 0;\n for(int i = 0; i < SIZE; i++) {\n sum += array[i];\n }\n return sum;\n}\n\n\/\/ S\u010d\u00edt\u00e1 dvourozm\u011brn\u00e9 pole\ntemplate<int SIZE, typename T>\nT soucet(T (*array)[SIZE]) {\n T sum = 0;\n for(int i = 0; i < SIZE; i++) {\n sum += soucet<SIZE>(*(array + i));\n }\n return sum;\n}\n\n\/\/ S\u010d\u00edt\u00e1 trojrozm\u011brn\u00e9 pole\ntemplate<int SIZE, typename T>\nT soucet(T (*array)[SIZE][SIZE]) {\n T sum = 0;\n for(int i = 0; i < SIZE; i++) {\n sum += soucet<SIZE>(*(array + i));\n }\n return sum;\n}\n\nconst int MAX_SIZE = 3;\n\ndouble pole[MAX_SIZE] = { 1.1, 2.2, 3.3 };\n\ndouble pole2D[MAX_SIZE][MAX_SIZE] = {\n { 1.1, 2.2, 3.3 },\n { 1.1, 2.2, 3.3 },\n { 1.1, 2.2, 3.3 }\n};\n\ndouble pole3D[MAX_SIZE][MAX_SIZE][MAX_SIZE] = {\n { { 1.1, 2.2, 3.3 }, { 1.1, 2.2, 3.3 }, { 1.1, 2.2, 3.3 } },\n { { 1.1, 2.2, 3.3 }, { 1.1, 2.2, 3.3 }, { 1.1, 2.2, 3.3 } },\n { { 1.1, 2.2, 3.3 }, { 1.1, 2.2, 3.3 }, { 1.1, 2.2, 3.3 } } \n};\n\nint main() {\n cout << "Soucet 1D pole je " << soucet<MAX_SIZE>(pole) << endl;\n cout << "Soucet 2D pole je " << soucet<MAX_SIZE>(pole2D) << endl;\n cout << "Soucet 3D pole je " << soucet<MAX_SIZE>(pole3D) << endl;\n return 0;\n}”,”https:\/\/www.jdoodle.com\/a\/ag7?format=text”:”#include <cstring>\n#include <iostream>\nusing namespace std;\n\ntemplate<typename T>\nbool isLess(T a, T b) {\n return a < b;\n}\n\nbool isLess(const char* a, const char* b) {\n return strlen(a) < strlen(b);\n}\n\nint main() {\n cout << boolalpha;\n cout << "1 < 3: " << isLess(1, 3) << endl;\n cout << "3.14 > 2.1: " << isLess(3.14, 2.1) << endl;\n cout << "\\’aaa\\’ < \\’bbbb\\’: " << isLess("aaa", "bbbb") << endl;\n cout << "\\’aaa\\’ > \\’bb\\’: " << isLess("aaa", "bb") << endl;\n cout << "1.111 < 2.222: " << isLess(1.111, 2.222) << endl;\n return 0;\n}\n\n”}


Šablony funkcí II
Ohodnoťte tento článek