怎样用cout 输出一个数组temp[]中的元素?
这个数组如果是字符型的话,只要cout << temp <<endl;就行了。
如果不是字符型的,要使用循环才能输出。
for ( int i = 0; i < sizeof(temp); i++)
{
cout << temp[i] << endl;
}
UPUPUP~~~
假如temp的声明为
Elem temp[size];
方法为:
1:
for (int i = 0; i < sizeof(temp)/size(Elem); ++i)
std::cout << temp << \n;
2:
std::copy(temp, temp+sizeof(temp)/size(Elem),
ostream_iterator<Elem>(cout, \n));
其中 \n为分割符号,你也可以替换为其他。
注意:
1: 长度为sizeof(temp)/sizeof(Elem), 而不是 sizeof(temp)
2: 不要经常调用cout << endl; 这会刷新流而减慢速度。
可以在操作完成后调用cout.flush();