CIS 22B - Notes for Tuesday, 10/20

Announcements and Reminders

Recording

Comments on Assignment 4 

Check your expected output
Use a second compiler

Vectors

A C++ vector is a class template, a container in the C++ Standard Template Library (STL).
  • Can be used as a replacement for an array.
  • Vectors do not have to be dimensioned.
  • Vectors grow automatically, as needed.
  • Memory allocated for a vector does not have to be released.  It is automatically when the vector goes out of scope.
  • Vectors can be indexed, just like an array.
  • To declare a vector, you must specify the type of data to be stored in the vector, using angle brackets.
  • Requires the <vector> header file

  Example

  vector<int> vint;          // vint is called a template class

  Some initial useful vector members

constructor (default)creates an empty vector
=assignment operator
size()returns the size of the vector
[ ]index operator (returns rvalue or lvalue)
push_back()adds an element to the end of the vector
pop_back()removes the element at the end of the vector

Reference

Example

C++ string class

Reference

Member Functions Prototype Description
constructor string();
string(const char* str);
string(const str& str);
Creates a string object
size
length
size()
length()
Returns the length of the string
clear clear() Clears the string
c_str c_str() Returns null-terminated char array contents of the string
find size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;    
size_t find (char c, size_t pos = 0) const;
Locates text in a string.  Returns npos if not found
substr string substr (size_t pos = 0, size_t len = npos) const; Returns a substring of the string object
Member Operators
= assignment operator= (const string& str);    
string& operator= (const char* s);
string& operator= (char c);
Assigns a value to a string
[ ] index       char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
Returns the char at a location in the string
+= plus equal string& operator+= (const string& str);
string& operator+= (const char* s);
string& operator+= (char c);
Concatenates text to a string
Non-member Operators
+  plus string operator+ (const string& lhs, const string& rhs);
string operator+ (const string& lhs, const char*   rhs);
string operator+ (const char*   lhs, const string& rhs);    
string operator+ (const string& lhs, char          rhs);
string operator+ (char          lhs, const string& rhs);
Concatenates a string and text.  Returns a new string.
Non-member functions
getline istream& getline (istream& in, string& str, char delim);
istream& getline (istream& in, string& str);
Reads from an input stream into a string
Member Constants
npos Indicates not found

Example

Lab Exercise #5

Put your name, the compiler used, and Lab Exercise #5 in a comment at the top of your program. Email your source code.

Write a program to read the input file, shown below and write out the output file shown below.  Use only string objects and string functions to process the data.  Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution.  

Input File
Cincinnati 27, Buffalo 24
Detroit 31, Cleveland 17
Kansas City 24, Oakland 7
Carolina 35, Minnesota 10
Pittsburgh 19, NY Jets 6
Philadelphia 31, Tampa Bay 20
Green Bay 19, Baltimore 17
St. Louis 38, Houston 13
Denver 35, Jacksonville 19
Seattle 20, Tennessee 13
New England 30, New Orleans 27
San Francisco 32, Arizona 20
Dallas 31, Washington 16


Output File
Cincinnati over Buffalo by 3
Detroit over Cleveland by 14
Kansas City over Oakland by 17
Carolina over Minnesota by 25
Pittsburgh over NY Jets by 13
Philadelphia over Tampa Bay by 11
Green Bay over Baltimore by 2
St. Louis over Houston by 25
Denver over Jacksonville by 16
Seattle over Tennessee by 7
New England over New Orleans by 3
San Francisco over Arizona by 12
Dallas over Washington by 15