关于c ++的问题.......................... [英] question about c++..........................
本文介绍了关于c ++的问题..........................的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int abc [] = { 5 , 8 , 9 };
int i;
for (i = 0 ; i< =; i ++)
cout< < abc [i];
我的问题我写的条件是什么值
解决方案
数组中有3个项目,在这种情况下,索引可以是0,1或2.
您可以进行以下测试:
for (i = 0 ; i< = 2 ; i ++)
或
for (i = 0 ; i< 3; i ++)
或
for (i = 0 ; i< ; sizeof(abc)/ sizeof(abc [ 0 ]); i ++)
只能使用最后一个,因为abc的数组大小是在变量的定义中设置的(在这种情况下隐含的,explicite将是in t abc [3];)。
它不能用于:
int NbItems( int abc []){ return sizeof (abc)/ sizeof(abc [ 0 ]);}
int * abc; abc = new int [ 3 ];
我认为你可以使用<> BR />
int nSize = sizeof (abc)/ sizeof(abc [ 0 ]);
所以代码如下所示
#include < iostream.h >
int main()
{
int abc [] = { 5 , 8 , 9 跨度>};
int i;
int nSize = sizeof (abc)/ sizeof(abc [ 0 ]);
for (i = 0 ; i< nSize; i ++)
{
cout<< abc [i];
}
返回 1 ;
}
有(I = 0; I<的sizeof(ABC)/的sizeof(ABC [0]);我++)
int abc[]={5,8,9};
int i;
for(i=0;i<= ;i++)
cout<<abc[i];
my qestion what value i write in condition
解决方案
You have 3 items in array, in this case, index can be 0, 1 or 2.
You can do the following test :
for(i=0;i<=2;i++)
or
for(i=0;i<3;i++)
or
for(i=0;i<sizeof(abc)/sizeof(abc[0]);i++)
the last one can only be used because the array size of abc is set at the definition of the variable (implicitely in this case, explicite will be "int abc[3];").
It cannot be used with those:
int NbItems(int abc[]) { return sizeof(abc)/sizeof(abc[0]);} int* abc; abc = new int[3];
I think you can use
int nSize = sizeof(abc)/sizeof(abc[0]);
so the code is look like below
#include <iostream.h> int main() { int abc[]={5,8,9}; int i; int nSize = sizeof(abc)/sizeof( abc[0] ); for(i=0; i < nSize ;i++ ) { cout<<abc[i]; } return 1; }
try it
http://stackoverflow.com/questions/2773328/how-to-find-the-size-of-integer-array
for(i=0;i<sizeof(abc)/sizeof(abc[0]);i++)
这篇关于关于c ++的问题..........................的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文