Week 8 Lecture Summary for CSC 309

These notes are not intended to be complete. They serve as an outline to the class and as a supplement to the text.


STL Map Class

Creating a Map

Must specify the type of the key and the value when constructor a map object.

Consider counting words in a file.

#include <map>
using namspace std;

map<string, int> wordCount;

Inserting Elements into a Map

 wordCount["guitar"] = 1;
 wordCount["java"];

Traversing a Map

for(map<string,int>::iterator it = wordCount.begin(); it != wordCount.end(); ++it){
       cout <<"(" << it -> first <<", " << it -> second << ")" << endl;
 } 

Note: when dereferencing the iterator you get a reference to the pair object.

Search, Find and Lookup

++wordCount["guitar];  // now value is 2
++wordCount["java"];   // now value is 1

if( wordCount["guitar"] > 2) {
    :
    :
}
  • If the key does not exist, operator[] will insert the key into the list.
  • Use method find to determine if a key is in the map.
  • Method find returns an iterator to the pair object if the key exists, else returns end()
    map<string, int>::iterator isIt = wordCount.find("java");
    
    if(isIt != wordCount.end()){
       cout << "its there, and the value is " << isIt -> second << endl;
    }
    

    Remove

    wordCount.erase("java");
    

    Another Way to Insert

    wordCount.insert(make_pair("piano",5));
    

    The insert method returns a pair object whose key is an iterator to the inserted object and whose value is a boolean indicating if the pair was actually inserted.

    To check for insertion, we do

    pair< map<string, int>::iterator, bool> returnPair;
    returnPair = wordCount.insert(make_pair("bassoon",50));
    
    if(returnPair.second){
       cout << "insert accomplished";
    }
    

    BookStore Program

    Write a program that allows the user to interact with a collection of Books. Insert books, find books, remove books etc.. The books will be loaded and saved to the file books.txt

    Two solve this program we will use the following two user define classes.

    Notes:

    The program will divided into 4 files.

    To Run Programs Using Multiple User Defined Files in DEV-C++

    Make sure you close all previous files and projects. File -> Close all, Close projects.

    To Run Programs Using Multiple User Defined Files under Linux

    Macro Guards


    Function Templates

    void swap(int &first, int &second){ int temp = first; first = second; second = temp; }
    void swap(char &first, char &second){ char temp = first; first = second; second = temp; }
    template <typename T>
    void swap(T &first, T &second){
       T temp = first;
       first = second;
       second = temp;
    }
    
    int x(5), y(10);
    char let1('a'), let2('z');
    string s1("tuesday"), s2("sunday");
    
    swap(x,y);         
    swap(let1,let2);
    swap(s1,s2):
    
    template <typename Type>
    void swap(Type &first, Type &second){
       Type temp = first;
       first = second;
       second = temp;
    }
    
    OR
    
    template <typename G>
    void swap(G &first, G &second){
       G temp = first;
       first = second;
       second = temp;
    }
    
    template <class T>
    void swap(T &first, T &second){
       T temp = first;
       first = second;
       second = temp;
    }