Добавил:
vvrstcnho
Рад, если кому-то помог
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы С++ (для ИВТ) / Готовые лабы С++ / Лаба8 / Laba 8 (2)
.cpp#include <iostream>
#include <cstring>
#include <locale>
using namespace std;
// Базовая шаблонная функция
template<typename T>
void copy(T dest[], T source[], int size) {
for (int i = 0; i < size; i++) {
dest[i] = source[i];
}
}
// Специализация для строк
template<>
void copy<char*>(char* dest[], char* source[], int size) {
for (int i = 0; i < size; i++) {
strcpy(dest[i], source[i]);
}
}
// Специализация для const char*
template<>
void copy<const char*>(const char* dest[], const char* source[], int size) {
// Для const char* копирование указателей
for (int i = 0; i < size; i++) {
dest[i] = source[i];
}
}
int main() {
setlocale(LC_ALL, "ru_RU.UTF-8");
// Тестирование с int
int src_int[] = {1, 2, 3, 4, 5};
int dest_int[5];
copy(dest_int, src_int, 5);
cout << "Скопированные int: ";
for (int i = 0; i < 5; i++) cout << dest_int[i] << " ";
cout << endl;
// Тестирование с double
double src_double[] = {1.1, 2.2, 3.3, 4.4, 5.5};
double dest_double[5];
copy(dest_double, src_double, 5);
cout << "Скопированные double: ";
for (int i = 0; i < 5; i++) cout << dest_double[i] << " ";
cout << endl;
// Тестирование со строками
char* src_str[] = {(char*)"hello", (char*)"world"};
char* dest_str[2];
dest_str[0] = new char[10];
dest_str[1] = new char[10];
copy(dest_str, src_str, 2);
cout << "Скопированные строки: " << dest_str[0] << " " << dest_str[1] << endl;
delete[] dest_str[0];
delete[] dest_str[1];
return 0;
}
Соседние файлы в папке Лаба8
