C++ 数组输出

C++中输出数组数据分两种状况:字符型数组和非字符型数组html

当定义变量为字符型数组时,采用cout<<数组名; 系统会将数组看成字符串来输出,如:数组

1 char str[10]={'1','2'};
2 cout << str <<endl ; //输出12

若是想输出字符数组的地址,则须要进行强制转换,如:spa

1 char str[10]={'1','2'};
2 cout << static_cast <void *> (str) <<endl ; //按16进制输出str的地址,如:0012FF74

当定义变量为非字符符数组时,采用cout<<数组名; 系统会将数组名看成一个地址来输出,如:code

1 int a[10]={1,2,3};
2 cout << a <<endl ; //按16进制输出a的值(地址)    0012FF58

若是须要输出数组中的内容,则须要采用循环,逐个输出数组中的元素,如:htm

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循环的其余用法blog

1 for (auto i :a)
2     cout<<i<<endl;

原文出处:https://zhidao.baidu.com/question/28706144.html字符串

相关文章
相关标签/搜索