Main

Lab 7

Objective:

STL containers, iterators, and algorithms.

You may Google each function or class for more examples and learn how to use them. As always, refer to http://en.cppreference.com/w/cpp for complete details. The http://www.cplusplus.com/ website is also a good resource for STL.

  • Define a std::vector<int> variable v and add 15 random integers into that container. Use the <random> header file and the “uniform_int_distribution” to generate values between 1 and 100, inclusive (http://www.cplusplus.com/reference/random/uniform_int_distribution/). <random> is available only in C++11 and newer. If you prefer, you can use floating-point values instead of ints, in that case, look up uniform_real_distribution -- if you decide to do that, use float or double instead of int in all subsequent parts of the lab.
  • Write a print function that accepts const std::vector<int> & and prints the elements of the vector, using the iterator std::vector<int>::const_iterator. For this function, you should not use the index operator [] directly (when using a for loop, the performance of the iterator is typically better than the [] operator - why? Optionally, measure the time each approach takes (using [] or iterators) -- note that you may have to add an extra outer loop to repeat this many times since timing short runs is inaccurate; refer to the chrono documentation and examples).
  • Print the random values of the vector v, using your previously-defined function.
  • Optionally, you can use the matplotlib-cpp simple C++ library for plotting. First, download this version (it has a better makefile): https://ix.cs.uoregon.edu/~norris/cis330/materials/matplotlib-cpp.zip. To check whether it works on your machine, cd to matplotlib-cpp/examples and "make minimal", then run it with "./minimal" (a window with a jagged line should pop up, this may be pretty slow). To use in your code, copy the matplotlibcpp.h file to the lab7 directory, then #include "matplotlibcpp.h" and follow the matplotlib-cpp/examples/minimal.cpp example or any of the other examples.
  • Sort the values in v using the std::sort function, which is available in the <algorithm> header. Print the sorted result. Optionally plot the new values.
  • Define a new std::vector<int> variable vCopy. Use the std::copy function to make copy of v into vCopy. Print (and optionally plot) the vCopy contents.
  • Use the std::random_shuffle function to shuffle the values in vCopy. Note that the previous values in v ware sorted. Print (or plot) the values in vCopy.
  • If all of the above is too easy and you are done way early, work on the haiku exercise from the last lecture.

Green Marinee theme adapted by David Gilbert, powered by PmWiki