CS 112 - Week 9 Lecture 1 - 2022-10-18 TODAY WE WILL: * announcements * intro to C++ vector class * a few more standard output formatting tools * (if time) example of stringstream * prep for next class * should be working on Homework 7! * Current reading: * Savitch, Chapter 8, Section 8.3 - has an intro to the C++ vector class * Savitch, Chapter 2, Section 2.2 - has a few more standard output formatting tools * Savitch, Chapter 13 - his take on linked lists (but he takes a slightly different approach than we will) ===== Intro to the C++ vector class ===== * Runestone text also has a chapter on: Chapter 10 (with a few quirks...) * C++ has something called the Standard Template Library (STL) * purpose: to provide standard portable implementations of many classic data structures * often called container classes because they are used to hold collections of data * template? that means these are TEMPLATE CLASSES, collections of values where the user specifies the type of the elements in that collection when they declare an instance of that collection * so: vector is one of the STL classes. * to use a vector, you need to #include the vector class: #include <vector> * what kind of container IS a vector? * basically: a more convenient, more flexible array-type collection * it can "grow" and "shrink" and the class methods can hide all the annoying details that make that possible! * like an array, all the elements in a vector must be of the same type * DOES provide the [index] notation you already know for using elements within a vector * and, as a class, it has METHODS!!!! * how do you declare a vector? ...since it is a template class, you specify the type of elements in that vector by putting the type AFTER the class name IN a set of angle brackets: // this makes, from your perspective, // a vector worm_counts that can hold // int values and currently is empty vector<int> worm_counts; // this makes, from your perspective, // a vector worm_weights that can hold // double values and currently is empty vector<double> worm_weights; * vector method: size * expects no arguments, returns the size (the number of elements currently in) the calling vector * to increase the size of a vector, you call its push_back method, with a desired new element, and that element is added to the end of the vector, and the vector's size increases by 1 worm_weights.push_back(6.6); worm_weights.size() == 1 ...and you can access this using the familiar indexing syntax you use in arrays: worm_weights[0] == 6.6 * a couple of other available vector constructors: * a 1-argument constructor: lets you specify an initial size for your vector // grades is created with 47 elements, // and you can write grades[0] up // to grades[46] without having to // call push_back first vector<double> grades(47); * a 2-argument constructor: lets you specify an initial size, and a desired initial value for all of the vector's elements // vector destinations is of size 4, // and each has the initial value "TBA" vector<string> destinations(4, "TBA"); * you can reduce the size of a vector for example with its pop_back method: pop_back expects nothing, returns nothing, and has the size-effect of reducing the vector's size by 1 and removes the element on the end (and destroys it...?!) ===== a few more standard output formatting tools ===== * we know boolalpha, added to an output file stream, asks that bool values be formatted as true and false * here's how you can ask that double values be formatted to a specific fixed number of fractional places: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); ...now, until you change it or the function is done, cout will use this formatting for double values * you can also ask that values be right-justified in a field of a given size using the function setw * this is from C++ library iomanip, you DO need to #include <iomanip> !! * setw expects the desired output field width as an int and uses this field width for the NEXT value output to that stream (right-justifying values, and if too "small", setw is ignored) cout << setw(8) << angie.get_exp()