CSE 4208: Studio 17

Sequential Containers


Today's lecture and studio focus on how left-inclusive (a.k.a. closed-open) ranges can be used to structure iteration over different kinds of sequential containers, and how using pointers over a C-style array is comparable to using iterators provided by other sequential container types from the C++ standard library.


Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.

As you work through these exercises, please record your answers, and when you finish them please log into Canvas, select this course in this semester, and then upload a .txt file containing your answers on the Canvas page for this studio assignment. Only one submission per team, please, and if you need to re-submit it the person who originally submitted the studio should please be the one to do that.

Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.


Required Exercises

  1. As the answer to the first exercise, list the names of the people who worked together on this studio.
  2. First, ssh into shell.cec.wustl.edu using your WUSTL Key id and password and then use qlogin to log into one of the Linux Lab machines and then confirm that the version of g++ there is correct (as you did in Studio 0). Then cd into your directory for this course this semester, create a new subdirectory for this studio, and cd into it.

    Copy over the CMakeLists.txt, main source file, and main header file, from your solution for Studio 16 to your directory for this studio. Rename any source and header files as appropriate (e.g., studio16.h to studio17.h and studio16.cpp to studio17.cpp, etc.) and modify your CMakeLists.txt accordingly.

    Remove all contents from your main function except for the statement at the end that returns a descriptively named label with a value of 0 to indicate success, and remove any other function declarations and definitions (as well as any unnecessary header file include statements) from your main header and source files.

    In your main function declare a C-style array of int and initialize it with different values. After that your main function should output to the standard output stream the values returned by calling sizeof on the array variable, and by calling sizeof on the type the array contains. Compile and run your program, and as the answer to this exercise, please show (1) the output the program produced, and (2) your main function.

  3. Based on the values that were produced by the calls to sizeof operator in the previous exercise, figure out an expression that uses those calls, which should be added to the starting address of the array to initialize a pointer that points just past the end of the array; using the name of the array and that formula, write a for loop that uses only the ++, ==, and ! operators to move a pointer to int from start of the array to just past the end of the array, and prints out each value in the array to the standard output stream.

    Compile and run your program, and as the answer to this exercise please show (1) the output the program produced, and (2) the code you added for this exercise.

  4. Add a template header and source file to your directory (and to your CMakeLists.txt) and in them declare and define a function template with a single type parameter that takes two variables of that type by value. Move the for loop out of your main function into that function template, replacing the int type with the parameterized type, initializing the pointer with the first variable that was passed in to the template, and halting the iteration when the pointer is equal to the second variable that was passed into the function template.

    In your main function, after the array is declared, pass the name of the array and the name of the array plus the formula from the previous exercise, into a call to your function template. Compile and run your program, and confirm that it produces the same output as in the previous exercise. As the answer to this exercise, please show the code for the definition of your function template and for the call to it in the main function.

  5. In your main function, declare a deque of int, a vector of int, and a list of int, and then write another for loop that iterates a pointer through the array and calls emplace_back on each of those container variables with the value of each element of the array.

    Then, call your function template with the iterators returned by calls to each containter's begin() and end() member functions, to print out their contents as well. Compile and run your program, and as the answer to this exercise please show the output the program produced.

  6. In your main function, declare an iterator for a vector of int, and intitialize it to point one position past the iterator returned by your vector container's begin() member function. Call your function template with the container's begin() member function and the iterator you declared, and then call your function template again with the iterator you declared and the vector container's end() member function. After that, reposition the iterator you declared to point to the middle of the vector's range, using random access iterator arithmetic: subtract the result of the vector's begin() member function from the result of its end() member function, divide that difference by two, and add the result of that division to the iterator returned by the vector's begin() member function.

    Again call your function template with the vector container's begin() member function and the iterator you declared, and then call your function template again with the iterator you declared and the vector container's end() member function.

    Compile and run your program, and as the answer to this exercise please show the code you added for this exercise, and the output that code produced.

  7. Write four separate statements that try to use the array indexing operator ([]) to access and print out (to the standard output stream) the first (0th) element in your array, your deque, your vector, and your list, commenting out any statements that will not compile.

    Compile and run your program, and as the answer to this exercise, please (1) say which of those containers support the array indexing operator, and (2) for the one(s) that do not support it, please explain briefly why they do not.

Things to Turn In:

For this studio, please turn in the following: