TC1033 Exercise

Here is a simple exercise to work on the concepts of Vectors, Lists and Iterators in C++.

Why do we want to do this instead of using arrays and for loops? We want to look towards fully embracing object-think and coding using object-oriented methods.

  1. Investigate the documentation for classes Vector, List and Iterator. See references below or find your own to suggest to the rest of the group.
  2. Create a program that creates a vector (of int) with the elements {1,2,3,4,5}. Can you do this at variable declaration?
  3. Extend your program to print (one per line) each of the elements of the vector with a for loop.
  4. Change the program to create an empty vector and then with a while loop, ask the user for integers to put in the vector (how do you add to it?) until the user enters a zero (0). Again, use the for loop to print them.
  5. Now do the same with a list (steps 2 to 4)
  6. Now, let’s remove the for loop and use an iterator (look up the Iterator class and find examples to help) to replace those loops that you used for printing.
  7. Feel proud, you are on your way to being an Object Master.

References to check out.

Feel free to suggest others in the comments below.

Featured Image Credit

"young jedi master tess" Flickr image by https://www.flickr.com/photos/ohoski/
“young jedi master tess” flickr photo by Chimpr https://flickr.com/photos/ohoski/5065384918 shared under a Creative Commons (BY-NC-ND) license
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.