再帰によって配列を出力する関数を作成しようとしています。
3 つの関数を書きましたが、原理的には同じようです。
void printArray0( int theArray[], int theSize, int theIndex = 0 )
{
cout << theArray[theIndex] << ' ';
if ( ++theIndex != theSize ) printArray0( theArray, theSize, theIndex );
else cout << endl;
}
void printArray1( int theArray[], int theElementLeft )
{
cout << *theArray << ' ';
if ( theElementLeft != 1 ) printArray1( theArray+1, theElementLeft-1 );
else cout << endl;
}
void printArray2( int theArray[], int theSize )
{
static int myIndex = 0;
cout << theArray[myIndex] << ' ';
if ( ++myIndex != theSize ) printArray2( theArray, theSize );
else cout << endl;
}
それで、それらの間に大きな違いはありますか?
ある場合、どの機能が最も速く、最も安全ですか?
他の人の経験から学べることを願っています:)