![Correct](https://cms.psu.edu/images/misc/right.gif)
![Incorrect](https://cms.psu.edu/images/misc/wrong.gif)
![Ungraded](https://cms.psu.edu/images/misc/ungraded.gif)
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() ) |
![Correct](https://cms.psu.edu/images/misc/right.gif)
![Incorrect](https://cms.psu.edu/images/misc/wrong.gif)
![Ungraded](https://cms.psu.edu/images/misc/ungraded.gif)
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) |
![Correct](https://cms.psu.edu/images/misc/right.gif)
![Incorrect](https://cms.psu.edu/images/misc/wrong.gif)
![Ungraded](https://cms.psu.edu/images/misc/ungraded.gif)
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) |
![Correct](https://cms.psu.edu/images/misc/right.gif)
![Incorrect](https://cms.psu.edu/images/misc/wrong.gif)
![Ungraded](https://cms.psu.edu/images/misc/ungraded.gif)
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. |
![Correct](https://cms.psu.edu/images/misc/right.gif)
![Incorrect](https://cms.psu.edu/images/misc/wrong.gif)
![Ungraded](https://cms.psu.edu/images/misc/ungraded.gif)
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 |
![Correct](https://cms.psu.edu/images/misc/right.gif)
![Incorrect](https://cms.psu.edu/images/misc/wrong.gif)
![Ungraded](https://cms.psu.edu/images/misc/ungraded.gif)
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. |
![Correct](https://cms.psu.edu/images/misc/right.gif)
![Incorrect](https://cms.psu.edu/images/misc/wrong.gif)
![Ungraded](https://cms.psu.edu/images/misc/ungraded.gif)
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 -- |
![Correct](https://cms.psu.edu/images/misc/right.gif)
![Incorrect](https://cms.psu.edu/images/misc/wrong.gif)
![Ungraded](https://cms.psu.edu/images/misc/ungraded.gif)
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 -- |
![Correct](https://cms.psu.edu/images/misc/right.gif)
![Incorrect](https://cms.psu.edu/images/misc/wrong.gif)
![Ungraded](https://cms.psu.edu/images/misc/ungraded.gif)
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 |
![Correct](https://cms.psu.edu/images/misc/right.gif)
![Incorrect](https://cms.psu.edu/images/misc/wrong.gif)
![Ungraded](https://cms.psu.edu/images/misc/ungraded.gif)
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