C++小测2

 

Incorrect Ungraded
1.
Today's recitation is a quick introduction to iterators, which will be used extensively in the next couple homework assignments. An iterator is used to visit the elements of a container object, without revealing just how that container is implemented.

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?

iter != data.end()
 

Feedback: This is one reason why end() is not the same as the tail of the list.
 
Table for Individual Question Feedback
Points Earned: 1.0/1.0  
Correct Answer(s): while ( iter != data.end() )


Incorrect Ungraded
2.
You can either expect that your small program addition works and move on, or you can actually take the time to verify it. It would be good to note just how that print function actually obtains the data values it is displaying.

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?

*iter != 3 && iter != sample.end()
 

Feedback: This is not much different from searching an array.
 
Table for Individual Question Feedback
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)


Incorrect Ungraded
3.
The insert method for STL containers takes two arguments: an iterator indicating where to insert, and a value to insert. An example has already been provided to insert the value 4. The next task is to use this same method to add new values to the endpoints of the list.

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.

sample.insert(sample.begin(), 1); sample.insert(sample.end(), 6);
 
 
Table for Individual Question Feedback
Points Earned: 2.0/2.0  
Correct Answer(s): sample.insert(sample.begin(), 1); sample.insert(sample.end(), 6); (one each)


Incorrect Ungraded
4.
Make sure your program correctly placed the values into the deisred locations.

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?

Because begin() is the first element and end() is the place after the last element, if we define a function insert after an iterator, we can never insert an element as the first element of the vector and the place after the end() is also invalid.
 

Feedback: Having insert this way also makes affecting the endpoints easy.
 
Table for Individual Question Feedback
Points Earned: 2.0/2.0  
Correct Answer(s): Such a definition would never allow insertions before the first element in the list.


Incorrect Ungraded
5.
Now that all the values are in the data, one can use an iterator to visit them all. Already the print function will display all the values 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.

for (iter = sample.begin(); iter != sample.end(); ++iter) { int n = *iter; if (n % 2 == 0) { cout << n << " "; } } cout << endl;
 
 
Table for Individual Question Feedback
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


Incorrect Ungraded
6.
We will now write a similar loop that duplicates some of the values (so that the list contains additional data). But before doing so, there is an important thing to know about the insert method when it is applied to a vector.

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
It will be an infinite loop if it meets an even number.
 
 
Table for Individual Question Feedback
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.


Incorrect Ungraded
7.
Write a loop (similar to the one that displayed even values) that will duplicate the even values in place (i.e. insert a copy of every value at the location the iterator found it.

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.

for (iter = sample.begin(); iter != sample.end(); ++iter) { int n = *iter; if (n % 2 == 0) { iter = sample.insert(iter, n); ++iter; } }
 
 
Table for Individual Question Feedback
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 --


Incorrect Ungraded
8.
Only one bit of code is left to complete: code to remove all even numbers from the list.

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.

for (iter = sample.begin(); iter != sample.end(); ) { int n = *iter; if (n % 2 == 0) { iter = sample.erase(iter); } else { ++iter; } }
 
 
Table for Individual Question Feedback
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 --


Incorrect Ungraded
9.
Let us actually verify that we have good information-hiding in our iterator.The edits you make here should not be included in the submission you just made,but are just here to experiment with.

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!

A) True
B) False
 
 
Table for Individual Question Feedback
Points Earned: 1.0/1.0  
Correct Answer(s): True


Incorrect Ungraded
10.
Hopefully, it still works. That would be an important note when we implement our own Iterators for the homework assignments. An iterator is introduced in Homework 2 to traverse all the elements in a linked list (the next course topic).

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?

void advance()
 
 
Table for Individual Question Feedback
Points Earned: 2.0/2.0  
Correct Answer(s): The ++ is represented by advance()

编程辅导qq 928900200

你可能感兴趣的:(C++小测2)