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++单元测试三大框架的比较软件测试1、TUT结构框架简单。添加新的测试工作量小;无须注册测试;可移植性好(因其只需两个头文件,就可以完成测试工作);便于装卸;提供接口可以扩展其输出方式等。最大的优点:轻量级,...
c++test测试工具
C++实现检测USB鼠标的拔插 本文将详细介绍如何使用 C++ 实现检测 USB 鼠标的拔插,包括代码实现和相关技术要点的讨论。 一、检测 USB 鼠标的拔插的需求 在 Windows 环境下,检测 USB 鼠标的拔插是许多应用程序的...
太小可能导致网络开销占比过大,太大则可能影响网络的正常传输。 2. **测试时间**:长时间的测试可以提供更稳定的带宽估计,但也会增加测试的复杂性。 3. **误差控制**:网络环境的波动、系统负载等因素可能影响...
桩函数是动态测试中的一个重要概念,它用于模拟被测代码依赖但无法直接控制的外部功能。C++ Test提供了两种类型的桩函数: - 用户自定义桩函数:以"CppTest_Stub_"为前缀,允许用户根据需求编写替换原有函数的行为...
openCV人脸检测的C++代码
根据提供的信息,我们可以总结出以下有关“C++小球碰撞”的相关知识点: ### C++小球碰撞概述 在计算机编程领域,“C++小球碰撞”通常指的是使用C++语言实现的小球之间的碰撞检测与响应机制。此类程序常用于游戏...
2. 如何使用 Parasoft C++ Test 工具来进行覆盖率测试 3. 回归测试的定义和目的 4. 如何使用 Parasoft C++ Test 工具来进行回归测试 5. 覆盖率测试和回归测试在软件测试中的应用 延伸阅读 1. Parasoft C++ Test ...
c++边缘检测代码。基于sobel算子的边缘检测
c++ 测试dll使用,用于java 调用c++ 测试 什么描述非得50字节 用于Java对接c++使用有需要的网友下载
Parasoft C++test 是经广泛证明的最佳实践集成解决方案,它能有效提高开发团队工作效率和软件质量。 C++test支持编码策略增强,静态分析,全面代码走查,单元与组件的 测试,为用户提供一个实用的方法来确保其C/C++...
行人检测C++代码
2. **小球对象**:定义一个`Ball`类,包含小球的位置、速度、半径等属性。初始化时,设定小球的初始位置和速度,比如沿着45度角的方向。 3. **运动更新**:在一个循环中,每次迭代都根据小球的速度更新其位置。速度...
用C++实现的死锁检测与解除算法,代码很短,很简单。
是用C++编写的一个完整的小学生四则运算测试的课程设计,里面有源码和已经编译好的debug文件,debug文件中可以直接运行,测试的结果可以存放到txt文本框中,可以在程序中查看参见测试的结果。这个课程设计是一个优秀...
3. **形态学操作**:形态学操作如腐蚀、膨胀、开闭运算等,有助于消除小瑕疵的干扰,分离真正的大瑕疵,并保持衣物边缘的连续性。这对于准确识别和分离真正的瑕疵至关重要。 4. **特征提取**:特征提取是识别瑕疵的...
基于qt+c++实现ddos小工具可用于网站压测等性能测试+源码,适合期末大作业、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 基于qt+c++实现ddos小工具可用于网站压测等性能测试+...
[2] 当日气温采用时间序列方法预测,即根据前面n 天的平均气温x1, x2, …, xn 预测当天的平均气温 y,即采用如下的预测模型: y = a0 + a1·x1 + a2·x2 + … + an·xn 其中系数 a0, a1, a2, …, an 根据最小二乘法...
测试工具,有关测试C++ 具,通用测试C++工具测试C++工具测试C++工具
在C++编程中,检测端口是否被占用是网络编程中的常见需求,特别是在服务器开发或者客户端连接时,确保端口可用性至关重要。本教程将详细解释如何使用C++实现这一功能,主要涉及TCP协议和系统调用。 首先,我们要...