C++ 复制 double 数组函数 - 代码示例
下面是一个使用 C++ 编写的复制 double 数组的函数示例:
#include <iostream>
void copyArray(const double* src, double* dest, int size) {
for (int i = 0; i < size; i++) {
dest[i] = src[i];
}
}
int main() {
const int size = 5;
double source[size] = {1.1, 2.2, 3.3, 4.4, 5.5};
double destination[size];
copyArray(source, destination, size);
std::cout << "Source Array: ";
for (int i = 0; i < size; i++) {
std::cout << source[i] << " ";
}
std::cout << "\nCopied Array: ";
for (int i = 0; i < size; i++) {
std::cout << destination[i] << " ";
}
return 0;
}
上述代码中,copyArray函数接收一个源数组指针(src)、一个目标数组指针(dest)和数组大小(size)。它通过使用循环遍历源数组,并将每个元素复制到目标数组中。
在主函数中,我们声明了一个大小为5的源数组(source)和一个目标数组(destination)。然后,我们调用copyArray函数将源数组复制到目标数组中。最后,我们分别打印源数组和复制后的目标数组。
输出应为:
Source Array: 1.1 2.2 3.3 4.4 5.5
Copied Array: 1.1 2.2 3.3 4.4 5.5
原文地址: https://www.cveoy.top/t/topic/jM4n 著作权归作者所有。请勿转载和采集!