下面是代碼:既然我們說加NULL(因爲我們在最後一個NULL字符,沒有在迭代問題)如何使用整數指針操作時確定整數數組的末尾?
char myChar[] ={ 'A', 'B', 'C', 'D', 'E', '\0' };
char *myCharPtr = &myChar[0];
while(*myCharPtr != NULL)
{
cout<<*myCharPtr<<endl;
myCharPtr++;
}
Output: ABCDE
我的問題是:
int myInt[] ={ 1, 2, 3, 4, 5 };
int *myIntPtr = &myInt[0];
while(*myIntPtr != NULL)
{
cout<<*myIntPtr<<endl;
myIntPtr++;
}
Output: 12345....<junks>..........
字符數組字符作爲字符串的結尾,我們排除了這些問題! 如果萬一,規則是將0加到整數數組的末尾,我們可以避免這個問題。說啥?
std :: begin/end定義在哪裏?它是否帶有C++ 0x? – 2010-04-29 10:46:15
@克里斯蒂安:是的,還是有助力。 – UncleBens 2010-04-29 12:00:17
@Christian是的,這是與Range有關的C++的一個新特性。 Boost.Range包含了這個以及更多,簡化了很多序列上的算法。 – 2010-04-29 14:35:59