C++中輸出數組數據分兩種情況:字符型數組和非字符型數組
當定義變量為字符型數組時,采用cout<<數組名; 系統會將數組當作字符串來輸出,如:
1 char str[10]={'1','2'}; 2 cout << str <<endl ; //輸出12
1 char str[10]={'1','2'}; 2 cout << static_cast <void *> (str) <<endl ; //按16進制輸出str的地址,如:0012FF74
當定義變量為非字符符數組時,采用cout<<數組名; 系統會將數組名當作一個地址來輸出,如:
1 int a[10]={1,2,3}; 2 cout << a <<endl ; //按16進制輸出a的值(地址) 0012FF58
如果需要輸出數組中的內容,則需要采用循環,逐個輸出數組中的元素,如:
1 int a[10]={1,2,3}; //初始化前三個元素,其余元素為0 2 for( int i=0;i<10;i++ ) 3 cout << a[i] <<" " ; 4 cout <<endl ; //輸出結果:1 2 3 0 0 0 0 0 0 0
注:for循環的其他用法
1 for (auto i :a) 2 cout<<i<<endl;
原文出處:https://zhidao.baidu.com/question/28706144.html