数组分为字符数组与非字符数组。
在使用 cout 输出以上两者数组的代码却有所不同,直接上图:
字符数组:
#includeusing namespace std; int main() { char a[10] = { "abdad" }; cout << a << endl; return 0; }
非字符数组(此处举例整型数组):
以下使用快排做左奇右偶(不关注从小到大排序)的项目:
(1)直接使用cout << A << endl;
#include#include using namespace std; const int Max = 5; int main() { int A[Max] = { 1,2,3,4,5 }; int len = sizeof(A) / sizeof(int); int i = 0, j = len - 1; int temp = 0; while (i < j) { if (A[i] % 2 == 1) { i++; } else { temp = A[i]; A[i] = A[j]; A[j--] = temp; } } cout << A << endl; return 0; }
输出的是非字符数组的内存地址(每次运行会有所不同)。
(2)此处案列使用for函数输出
#include#include using namespace std; const int Max = 5; int main() { int A[Max] = { 1,2,3,4,5 }; int len = sizeof(A) / sizeof(int); int i = 0, j = len - 1; int temp = 0; while (i < j) { if (A[i] % 2 == 1) { i++; } else { temp = A[i]; A[i] = A[j]; A[j--] = temp; } } for (int i = 0; i < len; i++) cout << A[i] << ' '; //cout << A << endl; return 0; }
输出结果:



