COMP2004
Programming Practice
2002 Summer School


Kevin Pulo
School of Information Technologies
University of Sydney


(page 1)


Associative Containers




(page 2)


Maps




(page 3)


Simple Map Example


#include
#include
using namespace std;
int main() {
map count;
string s;
while (cin >> s) {
count[s] = count[s] + 1;
// OR: count[s]++;
}
}


(page 4)


Accessing map items




(page 5)


Map Iterator Example


#include
#include
using namespace std;
int main() {
map count;
// fill count as previously
for (map::const_iterator
i=count.begin(); i != count.end(); ++i)
cout << i->first << '\t'
<< i->second << endl;
}


(page 6)


Pair




(page 7)


Manually inserting into a map


map m;
  • then can insert a pair with
pair p("test", 42);
m.insert(p);
  • or more simply
m.insert(pair("test", 42));


(page 8)


Set




(page 9)


set example


#include
using namespace std;
int main() {
set nums;
int val;
while (cin >> val)
nums.insert(val);
for (set::const_iterator i =
nums.begin(); i != nums.end(); ++i)
cout << *i << endl;
}


(page 10)


Testing Existance




(page 11)


set find() example


set s;
set::const_iterator si = s.find("it");
if (si != s.end())
cout << "Found it" << endl;
else
cout << "Didn't find it" << endl;


(page 12)


Another set find()


int main() {
set nums;
int val;
while (cin >> val)
nums.insert(val);

if (nums.find(42) != nums.end())
cout << "42 entered sometime";
else
cout << "42 not entered";
}


(page 13)


map find() example


map m;
map::const_iterator mi
= m.find("it");
if (mi != m.end())
cout << "It is : " << mi->second
<< endl;
else
cout << "Didn't find it" << endl;


(page 14)


Another map find()


int main() {
map count;
string s;
while (cin >> s) count[s]++;
if (count.find("hello") != count.end())
cout << "You said hello";
else
cout << "You never said hello";
}


(page 15)


Removing from a map/set


m.erase(key);
m.erase(m.find(key));


(page 16)


multimap / multiset




(page 17)


multiset Example


#include
int main() {
multiset nums;
int val;
while (cin >> val)
nums.insert(val);
for (multiset::const_iterator i =
nums.begin(); i != nums.end(); ++i)
cout << *i << endl;
}


(page 18)


Exceptions




(page 19)


C++ Exceptions




(page 20)


Throwing An Exception


struct Domain_Error {
int number;
Domain_Error(int d) : number(d) { }
};

double square_root(int n) {
if (n < 0) {
throw Domain_Error(n);
}
return sqrt(n);
}


(page 21)


Catching An Exception


int main() {
int num;
while (cin >> num) {
try {
cout << square_root(num) << '\n';
} catch(Domain_Error e) {
cerr << e.number
<< " is not valid\n";
}
}
}


(page 22)


Uncaught exceptions


Aborted (core dumped)


(page 23)


Multiple Catchers


try {
// do something...
} catch (SomeType st) {
// handle SomeType exception
} catch (SomeOtherType sot) {
// handle SomeOtherType exception
} catch (...) {
// handle anything else
}


(page 24)


Multiple Catchers II



class IOError { };
class NoFileError : public IOError { };
try { } catch (IOError e) { }


(page 25)


Rethrowing Exceptions




(page 26)

try {
try {
throw 10;
} catch (int i) {
cerr << "Caught : " << i << endl;
throw 20.0;
} catch (...) {
cerr << "Won't reach" << endl;;
}
} catch (int i) {
cerr << "int : " << i << endl;
} catch (double d) {
cerr << "double : " << d << endl;
throw;
}


(page 27)


Exception Specifications




(page 28)


Function Pointers


void foo() throw (X);
void foo2();

void (*pf1)() throw (X,Y) = &foo; // OK
void (*pf2)() throw () = &foo; // error
void (*pf3)() throw (X) = &foo2; // error



(page 29)