【发布时间】:2014-04-26 23:07:18
【问题描述】:
我终于从 Python/PHP 等转向 C++。对于我的生活,我无法从 Jumping into C++ 中弄清楚这个程序是如何工作的。看起来,如果你将一个数组传递给一个函数,而不是作为参考,它仍然修改了那个数组?对我来说,似乎“数组”在 sort() 之后不应该改变,因为它不是参考。
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int findSmallestRemainingElement (int array[], int size, int index);
void swap (int array[], int first_index, int second_index);
void sort (int array[], int size)
{
for ( int i = 0; i < size; i++ )
{
int index = findSmallestRemainingElement( array, size, i );
swap( array, i, index );
}
}
int findSmallestRemainingElement (int array[], int size, int index)
{
int index_of_smallest_value = index;
for (int i = index + 1; i < size; i++)
{
if ( array[ i ] < array[ index_of_smallest_value ] )
{
index_of_smallest_value = i;
}
}
return index_of_smallest_value;
}
void swap (int array[], int first_index, int second_index)
{
int temp = array[ first_index ];
array[ first_index ] = array[ second_index ];
array[ second_index ] = temp;
}
// small helper method to display the before and after arrays
void displayArray (int array[], int size)
{
cout << "{";
for ( int i = 0; i < size; i++ )
{
// you'll see this pattern a lot for nicely formatting
// lists--check if we're past the first element, and
// if so, append a comma
if ( i != 0 )
{
cout << ", ";
}
cout << array[ i ];
}
cout << "}";
}
int main ()
{
int array[ 10 ];
srand( time( NULL ) );
for ( int i = 0; i < 10; i++ )
{
// keep the numbers small so they're easy to read
array[ i ] = rand() % 100;
}
cout << "Original array: ";
displayArray( array, 10 );
cout << '\n';
sort( array, 10 );
cout << "Sorted array: ";
displayArray( array, 10 );
cout << '\n';
}
【问题讨论】: