In this lesson, our instructor Alvin Sylvain goes through an introduction on containers: list. He starts by explaining arrays and vectors, linked list and list information, list access, and list iterators verse vector iterators. He then goes on to discuss simple list modification, complex list modification, functions as parameters, deque, stack and queue.
Since we'll need to confirm the contents of our list multiple times, it would be useful to create a function that does that. Write one that iterates through each element of a list<float>
container passed in as a parameter and prints out the value of each element. Hint: iterators are declared in the particular container, e.g., for a container declared as list<float>, the declaration for its iterator will be list<float>::iterator. Optionally, also pass in a string text parameter to print out what is being confirmed.
Create a program that declares a list for 10 floats. Use the list constructor to initialize it so that each element is set to 3.14. Confirm that each element was properly set.
Modify the program to set each element to 0.0. Confirm the change.
Modify the program to add 10 new elements, starting with a value of 0.5, and each element after the first one is 0.5 greater than the previous element. E.g., add element 0.5, 1.0, 1.5, etc. Use the “push_back()” method. Confirm the change.
Modify the program to add another 10 new elements, starting with a value of -0.5, and each element after that is 0.5 smaller than the previous element. E.g., add elements -0.5, -1.0, -1.5, etc. Use the “push_front()” method. Confirm as always.
At this point, your output should read something like;
-5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0 0 0 0 0 0 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Use the “remove_if()” method to remove every element with a value between -0.1 and 0.1. Confirm the results.
As it so happens, we’ve created a list that happens to already be sorted in ascending numerical order, so we don’t need to use the "sort()" method just now. Sorting large amounts of data can be time-consuming. We can avoid this so long as we always keep the list in order.
Modify your program to repeatedly ask the user for a number (stop looping when the user enters a 0.0). Insert this number into the list, but in the correct location in ascending numerical order. Confirm the results after the user is finished.
Hint: iterate through the list and inspect each element until you find an element value where the user’s input value is less.
Newsflash: we've just received a memo from the customer (You remember that guy, the one who pays our bills?) The list was supposed to be in descending numerical order, and he wants it yesterday. Rather than re-writing all the existing code, use the “reverse()” method after the insertions are completed.
Later, you can rewrite all the code if you have time.
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.