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.
- Investigate the documentation for classes Vector, List and Iterator. See references below or find your own to suggest to the rest of the group.
- Create a program that creates a vector (of int) with the elements {1,2,3,4,5}. Can you do this at variable declaration?
- Extend your program to print (one per line) each of the elements of the vector with a for loop.
- 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.
- Now do the same with a list (steps 2 to 4)
- 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.
- 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

Mentions