For this homework, to assure that the implementation details are hidden, out iter.cpp will use an iterator class already implemented in the C++ Standard Template Library. The details about templates are not important today.
We will start simply, by completing print() function at the top of the given program. Every container defines begin() and end() to mark the endpoints of the data.
begin() yields an iterator pointing to the first element.
end() yields an iterator that does NOT point to the last element, but just beyond it.
The loop given in the code says loop while false, which is clearly incorrect. What would be the loop condition for this loop?
Feedback: This is one reason why end() is not the same as the tail of the list.
Points Earned: | 1.0/1.0 | |
Correct Answer(s): | while ( iter != data.end() ) |
The rest of the assignment will consist of adding code to the main function.
The next small thing to do is to complete the first loop provided in the code, which is to search for a 3 in the data. This loop will actually have two loop conditions, since in general, we cannot always assume there actually is a 3 among that data. For full credit, both conditions must be tested together -- using break to leave this loop is not permitted.
What are the two different things to test when repeating this loop?
Feedback: This is not much different from searching an array.
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | That we have not gone off the end of the list ( iter != sample.end() ) and that we have not yet found our target value ( *iter != 3 ) (one point each) |
Show how one function call (and no loop) can insert a 1 at the front of this list, and how another single function call (and no loop) can place a 6 at the end.
Write the two relevant program statements in the answer box here, as well as in your code.
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | sample.insert(sample.begin(), 1); sample.insert(sample.end(), 6); (one each) |
Note that this insert method defines that a newly inserted value will be inserted before the value currently referred to by the iterator.
Now consider carefully the two newest statements in your previous answer.
Why would it not be appropriate to instead define that insert places the new value after the current value?
Feedback: Having insert this way also makes affecting the endpoints easy.
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | Such a definition would never allow insertions before the first element in the list. |
This time, where indicated, write code that displays all the even values (and only the even values).
Once you have that working, copy your loop into the answer box here.
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | for (iter = sample.begin(); iter != sample.end(); iter++) if (*iter % 2 == 0) cout << *iter << " "; a point for visiting all the data, and a point for displaying even values |
The vector is very much like an array whose size can grow as necessary. But if the memory following that vector is already in use by other variables, then the whole thing must be reallocated and copied elsewhere. Since the iterator is a value parameter to the insert method, it would no longer be able to refer to the actual vector.
Therefore, it would be good practice to use that return value to make sure the iterator is current.
But with that in mind, what danger would the following loop have?
for (iter = sample.begin(); iter != sample.end(); iter++) iter = sample.insert( iter, *iter ); // iter refers to newly inserted value
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | If the iterator points at the newly inserted value, which precedes the value we just duplicated, further loop repetitions would continually duplicate the same element, and we would have an infinite loop. |
For best results, use only the ++ and * operators on your iterator, and no +, - or --
Once you have it working correctly, copy the complete loop into the answer box.
Points Earned: | 3.0/3.0 | |
Correct Answer(s): | 1 point for clarity of code; 1 point for correctly incrementing the iterator for each insert; 1 point for only using ++ and *, and not + or - or -- |
In this case, it is especially important to update the iterator correctly. When you remove the value indicated by the iterator, then it is no longer among the data, and once that is the case, there is no longer any concept of what appears after it. Again, the return value from the defined method is necessary.
The erase method takes one argument: an iterator indicating which value to remove. It is defined to return an iterator referring to the value immediately after the one being removed. (It would not be good to continue to refer to a deleted object).
If you have any difficulty in removing all the even numbers, a sketch of how this return value works should be helpful.
HINT: It would be much simpler to use a while loop instead of a for loop, so that you have better control over when you use the ++ operator.
When you are finished, copy your answer into the answer box.
Points Earned: | 3.0/3.0 | |
Correct Answer(s): | 1 point for always using the erase return value; 1 point for getting all removals (by neglecting or cancelling the iterator increment); 1 point for using only ++ and *, and not + or - or -- |
So far this recitation stored all the data in an STL vector. Let us instead use the STL list.
The word vector appears five times in this source file (unless you added more usages). There is the #include at the top, two declarations in function print and two declarations at the top of function main.
Replace all those words vector with list, and try to rerun the program.
True/False: It still works!
B) False
Points Earned: | 1.0/1.0 | |
Correct Answer(s): | True |
The assignments will be implementing these iterators "from scratch" instead of using those from the Standard Template Library. But for now their use will be greatly simplified.
We will only use the iterators to traverse the data from beginning to end. Our containers will define begin() and end() as appropriate, but will not provide the insert or erase methods; the data will be for inspection only.
Unfortunately, not all of you would know how to define the * and ++ operators to use with iterators. So instead these operations will be given actual function names.
Take a peek at the proclist.h header file for Homework 2. What function within class ProcIterator seems to correspond to the ++ operator?
Points Earned: | 2.0/2.0 | |
Correct Answer(s): | The ++ is represented by advance() |
编程辅导qq 928900200
相关推荐
测试工具,有关测试C++ 具,通用测试C++工具测试C++工具测试C++工具
C++单元测试三大框架的比较软件测试1、TUT结构框架简单。添加新的测试工作量小;无须注册测试;可移植性好(因其只需两个头文件,就可以完成测试工作);便于装卸;提供接口可以扩展其输出方式等。最大的优点:轻量级,...
c++小学生数学测试程序完整版,文件保存、读取操作
C调用C++dll测试C调用C++dll测试C调用C++dll测试C调用C++dll测试C调用C++dll测试
openCV人脸检测的C++代码
C++ 单元测试 工程 可以自动生成测试工程,和测试类。 但必须先安装perl才可以哦!
关于使用Visual Studio进行C++单元测试, 关于使用Visual Studio进行C++单元测试
c++边缘检测代码。基于sobel算子的边缘检测
有关的一些相关的c++中相关的单元测试的技术和方法。主要针对c++喔!
简单的实现了我们平常进行白盒测试的基本实现方法以及结构,希望对你有帮助。
c++函数测试
c++ 测试dll使用,用于java 调用c++ 测试 什么描述非得50字节 用于Java对接c++使用有需要的网友下载
C++线程测试3!
ICRA的行人检测c++代码,linux下测试成功
C++实现检测USB鼠标的拔插的代码实现
c++转汇编的小测试,c++转汇编的小测试c++。c++转汇编的小测试
C/C++ 进程间 心跳检测 ,功能实现:可以检测 进程是否 live 或者 僵死,然后根据具体状态,发signal 处理被检测的进程
c++小效果, c++小测试代码,主要用到的system()函数
Parasoft C++test 是经广泛证明的最佳实践集成解决方案,它能有效提高开发团队工作效率和软件质量。 C++test支持编码策略增强,静态分析,全面代码走查,单元与组件的 测试,为用户提供一个实用的方法来确保其C/C++...
c++MFC,用于打开一个视频,检测烟火,有需要的,可以参考一下的