我正在学习“加速C++”。我有个关于问题5-3的问题。它要求:
5-3. By using a typedef, we can write one version of the program that implements either a
vector-based solution or a list-based one. Write and test this version of the program.'
下一个问题是:
5-4. Look again at the driver functions you wrote in the previous exercise. Note that
it is possible to write a driver that differs only in the declaration of the type for the data structure
that holds the input file. If your vector and list test drivers differ in any other way, rewrite them
so that they differ only in this declaration.
驱动函数到底是什么?通过创建if语句和重载函数来处理不同的数据类型,我已经解决了5-3问题,如下所示:
cout << "Please enter 1 if you would like to use vectors, or 2 if you would like to use lists: "<< endl;
int choose;
cin >> choose;
//CHOOSING TO USE VECTORS
if (choose == 1){....vector<Student_info> allStudents;
vector<Student_info> fail;.......}
//CHOOSING TO USE LISTS
else if (choose==2) {....list<Student_info> allStudents;
list<Student_info> fail;....}
//INVALID CHOICE
else {...invalid number, try again...}
除了重载的函数之外,我没有创建任何额外的函数来处理不同的数据类型。这些是司机的功能吗?如果没有,我一定是做错了问题。有人能开点光吗?:>
发布于 2014-01-12 00:58:11
在两个if
块中,在allStudents
和fail
上操作的代码有多相似,而不管它们是list
还是vector
?如果你完成了正确的作业,可能没有什么区别。现在,如果您删除该代码,删除对list
和vector
的引用,而在mytype
上操作,使用typedef vector<Student_info> mytype
或typedef list<Student_info> mytype
构建,您将得到他们所称的“驱动函数”。这不是一个正式的名字,你会找到互联网的参考。他们只是描述驱动list
和vector
操作以获得答案的代码。
发布于 2014-01-12 00:57:51
在这种特殊情况下,驱动程序代码是一种比较模糊的方法,可以说是测试代码。
换句话说,作者建议您查看一下用于验证在5-3中编写的代码的测试代码(又名驱动程序)。
https://stackoverflow.com/questions/21069897
复制相似问题