Start learning today, and be successful in your academic & professional career. Start Today!

• ## Related Services

### Start Learning Now

Our free lessons will get you started (Adobe Flash® required).

### Membership Overview

• *Ask questions and get answers from the community and our teachers!
• Practice questions with step-by-step solutions.
• Track your course viewing progress.
• Learn at your own pace... anytime, anywhere!

### Containers, List

• 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.
-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.

### Containers, List

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.

• Intro 0:00
• Lesson Overview 0:13
• Arrays and Vectors
• List Information 5:53
• Empty Method
• Max Size
• Resize
• List Access 7:48
• Front and Back
• Iterators
• Example
• List Iterators vs. Vector Iterators 9:24
• The Next Element May be Physically in Memory Before the Current Element
• Vector Iterators Allow Integer Add
• List Iterators Do Not Allow Integer Add
• Simple List Modification 14:24
• Constructors
• Difference Between Assign and Insert
• Assign
• Insert
• Push
• Pop
• Erase
• Complex List Modification 18:34
• Merging and Splicing
• Merge
• Splice
• Reordering
• Removing
• Modification Examples 23:22
• Functions as Parameters 24:49
• A Function for 'remove_it()'
• A Function for Merge and Sort - Evens Before Odds
• Deque 27:28
• Insertion or Removal in Middle May be More Efficient than with Vectors
• Not Guaranteed to be Contiguous Memory
• Same Method as Vector, Plus More
• Stack 29:11
• Empty, Size, Top, Push, Pop
• Queue 31:56
• Definition
• Not That Kind of List 33:19