Visual c++ 如何避免在此程序中使用指针变量和基于指针的传递引用?
如何避免在此程序中使用指针变量和基于指针的传递引用?正如我的导师所说,没有必要使用指针。这是一个龟兔模拟器,您将使用数字生成来模拟这个难忘的事件Visual c++ 如何避免在此程序中使用指针变量和基于指针的传递引用?,visual-c++,pointers,pass-by-reference,pass-by-pointer,Visual C++,Pointers,Pass By Reference,Pass By Pointer,如何避免在此程序中使用指针变量和基于指针的传递引用?正如我的导师所说,没有必要使用指针。这是一个龟兔模拟器,您将使用数字生成来模拟这个难忘的事件 #include <iostream> using std::cout; using std::endl; #include <cstdlib> using std::rand; using std::srand; #include <ctime> using std::time; #include <i
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>
using std::rand;
using std::srand;
#include <ctime>
using std::time;
#include <iomanip>
using std::setw;
const int RACE_END = 70;
// prototypes
void moveTortoise( int *const );
void moveHare( int *const );
void printCurrentPositions( const int *const, const int *const );
int main()
{
int tortoise = 1;
int hare = 1;
int timer = 0;
srand( time( 0 ) );
cout << "ON YOUR MARK, GET SET\nBANG !!!!"
<< "\nAND THEY'RE OFF !!!!\n";
// loop through the events
while ( tortoise != RACE_END && hare != RACE_END )
{
moveTortoise( &tortoise );
moveHare( &hare );
printCurrentPositions( &tortoise, &hare );
timer++;
} // end loop
if ( tortoise >= hare )
cout << "\nTORTOISE WINS!!! YAY!!!\n";
else
cout << "\nHare wins. Yuch.\n";
cout << "\nTIME ELAPSED = " << timer << " seconds" << "\n" << endl;
system("pause");
return 0; // indicates successful termination
} // end main
// progress for the tortoise
void moveTortoise( int * const turtlePtr )
{
int x = 1 + rand() % 10; // random number 1-10
if ( x >= 1 && x <= 5 ) // fast plod
*turtlePtr += 3;
else if ( x == 6 || x == 7 ) // slip
*turtlePtr -= 6;
else // slow plod
++( *turtlePtr );
if ( *turtlePtr < 1 )
*turtlePtr = 1;
else if ( *turtlePtr > RACE_END )
*turtlePtr = RACE_END;
} // end function moveTortoise
// progress for the hare
void moveHare( int * const rabbitPtr )
{
int y = 1 + rand() % 10; // random number 1-10
if ( y == 3 || y == 4 ) // big hop
*rabbitPtr += 9;
else if ( y == 5 ) // big slip
*rabbitPtr -= 12;
else if ( y >= 6 && y <= 8 ) // small hop
++( *rabbitPtr );
else if ( y > 8 ) // small slip
*rabbitPtr -= 2;
if ( *rabbitPtr < 1 )
*rabbitPtr = 1;
else if ( *rabbitPtr > RACE_END )
*rabbitPtr = RACE_END;
} // end function moveHare
// display new position
void printCurrentPositions( const int * const snapperPtr,
const int * const bunnyPtr )
{
if ( *bunnyPtr == *snapperPtr )
cout << setw( *bunnyPtr ) << "OUCH!!!";
else if ( *bunnyPtr < *snapperPtr )
cout << setw( *bunnyPtr ) << 'H'
<< setw( *snapperPtr - *bunnyPtr ) << 'T';
else
cout << setw( *snapperPtr ) << 'T'
<< setw( *bunnyPtr - *snapperPtr ) << 'H';
cout << '\n';
} // end function printCurrentPositions
#包括
使用std::cout;
使用std::endl;
#包括
使用std::rand;
使用std::srand;
#包括
使用std::时间;
#包括
使用std::setw;
常数int RACE_END=70;
//原型
无效移动乌龟(整数*常数);
无效移动份额(整数*常数);
无效打印当前位置(常量int*const,常量int*const);
int main()
{
int乌龟=1;
int=1;
int定时器=0;
srand(时间(0));
C++中的cUT可以使用引用代替指针,例如,代替
void foo(int *x) {
*x = *x + 1;
}
int main() {
int a = 0;
foo(&a);
return 0;
}
可以通过引用传递x,如下所示:
void foo(int &x) {
x = x + 1;
}
int main() {
int a = 0;
foo(a);
return 0;
}
传递引用有点像传递指针,只是不需要每次访问指针指向的值时都取消对指针的引用。
您可以在谷歌上搜索“C++参考传递”以获取更多信息,例如本教程:
或者,在程序中,只需传递int
参数并返回新值:
int moveTortoise(int turtle) {
...
turtle = turtle + 3;
...
return turtle;
}
tortoise = moveTortoise(tortoise)
当:
1.处理复杂类的实例通过引用传递是消耗资源(CPU时间和主内存)的操作;
当您想改变参数时,传递的< /强>(因为C++中的任何函数只能返回一个值,OpPosit为Ext。在Python上可以返回多值,通过使用“and”或“*”可以处理该限制;
3.其他案件
内置(原子)类型可以按值传递(这是您的情况),而不会降低效率