ecco il codice:Come determinare la fine di un array intero quando si manipola con un puntatore intero?
int myInt[] ={ 1, 2, 3, 4, 5 };
int *myIntPtr = &myInt[0];
while(*myIntPtr != NULL)
{
cout<<*myIntPtr<<endl;
myIntPtr++;
}
Output: 12345....<junks>..........
Per array di caratteri: (Dal momento che abbiamo un carattere null alla fine, non è un problema, mentre l'iterazione)
char myChar[] ={ 'A', 'B', 'C', 'D', 'E', '\0' };
char *myCharPtr = &myChar[0];
while(*myCharPtr != NULL)
{
cout<<*myCharPtr<<endl;
myCharPtr++;
}
Output: ABCDE
La mia domanda è dato diciamo per aggiungere NULL carattere come la fine delle stringhe, escludiamo tali problemi! Se nel caso, è la regola per aggiungere 0 alla fine dell'array intero, avremmo potuto evitare questo problema. Cosa dire?
Dove è definito std :: begin/end? Arriva con C++ 0x? –
@Christian: Sì o con Boost. – UncleBens
@Christian Sì, questa è una nuova funzionalità di C++ relativa a Range. Boost.Range include questo e molto altro, semplificando molto l'algoritmo sulle sequenze. –