運行結果:
may be you are right
may be you are right
9 7 6 5 4 3 2 1 10 66
9 7 6 5 4 3 2 1 10 66
Program ended with exit code: 0
Test.cpp
#include <iostream>
using namespace std;
template <typename T>
void show(T a, size_t);
template <typename T>
void show(T a);
int main() {
int a[] = {9, 7, 6, 5, 4, 3, 2, 1, 10,66};
int b[sizeof(a) / sizeof(int)] = {}; // 建立同數組a相同大小的數組b並將其所有元素初始化爲0
char aa[] = "may be you are right";
char bb[strlen(aa) + 1]; // 多分配一個字節存儲截止符'\0'
memcpy(bb, aa, strlen(aa) + 1);
memcpy(b, a, sizeof(a)); // 因爲memcpy是逐字節拷貝,而數組b爲int類型,拷貝的長度應爲sizeof(a)
show(a, sizeof(a) / sizeof(int));
show(b, sizeof(b) / sizeof(int));
show(aa);
show(bb);
return 0;
}
template<typename T>
void show(T a, size_t len) {
for (int i = 0; i < len; i++)
cout << a[i] << ' ';
cout << endl;
}
template<typename T>
void show(T a) {
while ((*a) != '\0')
cout << *a++;
cout << endl;
}