Download presentation
Presentation is loading. Please wait.
1
CS3101-2, Lecture 5 CS3101-2 Programming Languages – C++ Lecture 5 Matthew P. Johnson Columbia University Fall 2003
2
CS3101-2, Lecture 5 Agenda hw3 was due last night Today: Templates Exceptions Other odds and ends The STL Grading and the final hw4 TBA tonight
3
CS3101-2, Lecture 5 Templates Often want to do basically the same thing with different things functions work on variables only types specified algorithmic thinking computer science “functionalism” in phil. of mind abstraction human = “the rational animal” (Aristotle) Sometimes want to do basically the same thing with different types of things Queue of ints, queue of widgets “abstract data types”
4
CS3101-2, Lecture 5 max functions Suppose want the max of two numbers What kind of numbers? ints chars floats doubles All! How?
5
CS3101-2, Lecture 5 max functions Soln 1: Write one, maxly general function double max(double a, double b) { return a > b ? a : b; } double x = max(2.5, 3.5); char c = (char)max(‘A’,’B’); This works but it’s not nice All four types can widen to doubles but must be cast back
6
CS3101-2, Lecture 5 max functions Soln 2: Write one function for each type int max(int a, int b) { return a > b ? a : b; } double max( … etc. Is allowed in C++ (though not in C) But manually duplicating code for nontrivial ftns – bad hard to maintain
7
CS3101-2, Lecture 5 max functions Soln 3: Use the C preprocessor macros #define max(a,b) (a > b ? a : b) C source code is preprocessed #include s replaced with header files #ifndef, etc. macro calls replaced with macro content int c = max(2,3); int c = (2 > 3 ? 2 : 3); Works too, but complications, e.g.: z = max(x++, y++) z = (x++ > y++ ? x++ : y++) x, y inc-ed twice Need many parens – sq(a+b), etc.
8
CS3101-2, Lecture 5 max functions Soln 4: Use the CPP in a more sophisticated way Don’t use the CPP to generate expressions but to generate functions #define define_max(t)\ t max(t a, t b) {\ return a > b ? a : b;\ } define_max(char) define_max(int) etc. – no ; Avoids prev. CPP problems But reqs code for all poss types Done manually
9
CS3101-2, Lecture 5 Templates template result ftn(param-list) {…} The place-holder for the substituted type is t template and class are used as keywords can use typename in place of class T is a type Primitive or class All occurrences in ftn replaced with real type
10
CS3101-2, Lecture 5 max functions Soln 5: use templates parameterized function expands per type as necessary template T max(T a, T b) { return a > b ? a : b; } Now can simply call the ftn: x = max(2.5,3.5); Compiler autoly creates only the ftn specializations needed
11
CS3101-2, Lecture 5 Sorting things Consider problem of sorting: Sorting ints Sorting doubles Sorting strings Sorting widgets Point of sorting: put list in order Q: What does “in order” mean? A: Given an ordering relation < on the members For x and y, tells whether x < y Reorder s.t. x is before y iff x < y
12
CS3101-2, Lecture 5 Generic sorting Sort alg doesn’t depend of element type Merge sort, quick sort, etc. Need only give means to compare to elms How? In C we pass in a pointer to a compare ftn: void qsort(void *base, int n, int size, int (*cmp)(const void *, void *)); Pass in pointer to ftn: int cmp(const void *a, void *b) { Widget* w1 = (Widget*)a; … } Works, but very awkward
13
CS3101-2, Lecture 5 Generic sorting In Java, we pass a Comparable implementer In the sort ftn we say if (a.compareTo(b) < 0) … // means a < b Objects must implement this interface compare with ftn call Primitives can’t implement Compared with ops could put in wrappers…
14
CS3101-2, Lecture 5 Generic sorting C++ soln 1: Define our own Comparable analog: abstract class Has virtual compareTo Or, better: has virtual operators Any class extending our class can now be sorted Pass in array of Comparable-extending objects Sort uses polymorphism to treat as (mere) Comparables Downside: can only sort objects if they extend Comparable Mult inher: can always add Comp parent, but must do so To sort primitives must create wrapper classes
15
CS3101-2, Lecture 5 Generic sorting C++ soln 2: use templates! Let sort take an array of some arb. kind Don’t need Comparable Don’t need compareTo In sort, just say if (a < b) … If these are numbers, this works If these are objects that overload ops, this works Only requirement: kind supports ops
16
CS3101-2, Lecture 5 Templates: swapping Remember our swap-with-ptrs ftn? void swap(int &a, int &b) { int temp c = a; a = b; b = a; } Suppose we want to swap other types templates
17
CS3101-2, Lecture 5 Generic swapping template void swap(T &a, T &b) { T temp c = a; a = b; b = a; } Now can swap any prim Can also swap any objects As long as = op is public
18
CS3101-2, Lecture 5 Fancier swapping Remember our fancier swap ftn? void swap(int &a, int &b) { a ^= b ^= a ^= b; } Fancier template function: template void swap(T &a, T &b) { a ^= b ^= a ^= b; } Now can swap ints, chars, longs But: cannot swap objects Unless their ^= is overloaded – unlikely
19
CS3101-2, Lecture 5 Template specialization string s,t … max(s,t); works But max(“hi”,”there”) doesn’t: if (“hi” < “there”) … compares two pointers - where the char[] s start Not what we mean Soln: create a specialization special version for this case We check for spec. before template char *max(char *a, char *b) { return strcmp(a,b) > 0 ? a : b; }
20
CS3101-2, Lecture 5 Class templates Couple weeks ago: wrote a stack class supported only integers We’ll abstract element type away “Abstract data types” Only changes to declar: 1. prepend on class dfn: template class className {…} 2.Replace int T For ftn implems, we 1. prepend the same 2. replace className with className 3. Replace int T template void Stack ::push(const T elm){} To instantiate: Stack strStack;
21
CS3101-2, Lecture 5 Class specialization Similarly, can specialize member functions of class templates: void stack ::push(const char *const item) { data[count++] = item; }
22
CS3101-2, Lecture 5 Templates & statics Review: static data members one inst shared by all class insts What about statics in templates classes? Q: Could one inst be shared by all insts? A: No – consider: template class C { static T mem; … } mem couldn’t me shared by all insts shared by all insts But: for C, mem shared by all C insts
23
CS3101-2, Lecture 5 Templates & friends Given class, can declare some outside ftn or class its friend We have a stack class Suppose: want to declare external sort ftn its friend Before: had stack with ints could use sort ftn based on ints Now: have Stack friend is template too template class Stack { friend void C ::f5(X ); …
24
CS3101-2, Lecture 5 Odds and ends: Forward declarations Suppose classes Cat and Dog each depend on each other class Cat { void look(Dog d) { cout << “Meow!\n”; } }; class Dog { void look(Cat c) { cout << “Bark!\n”; } }; Q: Will this compile? A: No - Dog is referenced before declared Soln: a forward declaration Put class Dog ; before Cat def Dog not yet complete but Dog will now be recognized, w/o Cat depend.
25
CS3101-2, Lecture 5 Namespaces int i; namespace Example { double PI = 3.14; int i = 8; void printVals(); namespace Inner { int i = 9; } } // no semi-colon! Can access: Example::i, Example::Inner::i printVals implementation: void Example::printVals() { i is Example::i ::i is the global I }
26
CS3101-2, Lecture 5 Namespaces Now: can use Example Example::Inner Example::Inner::i Example::i Nested namespaces ~ Java packages Unfortly: #include (CPP) / using (C++) independent In general, use maximally narrow ranges Prevent ambiguity Don’t say using namespace std; Or can fully specify reference: std::std << std::endl;
27
CS3101-2, Lecture 5 assert Old days: bad thing happens writing to bad memory address divide by 0, etc. core dump, maybe don’t notice, etc. void Stack::push(const int item) { data[count++] = item; } no room overwrite wrong data, crash, etc. Somewhat better: assert that everything is okay assert(count >= 0 && count < sizeof(data)/sizeof(data[0])); “Everything’s okay, right?” If false, we quit with message of false expression
28
CS3101-2, Lecture 5 Exceptions Now: to some extent bad behavior is prevented attempt “exception” If bad things happen we halt, tell calling ftn maybe it halts, tells its calling ftn eventually, either someone responds accordingly or main ftn passes to OS try – throw – catch try to do something maybe an exception gets thrown if so, we may catch it, and go on
29
CS3101-2, Lecture 5 Exception handling void Stack::push(const int item) throws BoundExp { if (count = sizeof(data)/sizeof(data[0])) throw BoundExp(“stack overflow”); data[count++] = data; //ok if here } What is BoundExp ? A class we define
30
CS3101-2, Lecture 5 Our exception class BoundExp: exception { public: BoundExp(const string &s) exception(s) {} }; NB: It’s just a class Its parent is exception but needn’t be Exception has what() maybe other info
31
CS3101-2, Lecture 5 Throwing and catching try { Stack s; … s.push(25); … } catch (BoundExp &exp) { cout << “Error: “ << exp.what() << ‘\n’; } catch (ExpType2 &exp) { // can catch mult kinds // only catch <= 1 … } catch (…) { // … is a wildcard! cout << “Unknown exception caught.\n”; }
32
CS3101-2, Lecture 5 Exception classes exception runtime_error, logic_error bad_alloc: new failed bad_cast: dynamic_cast failed Can throw non-exception objs And even primitives But handling easer if don’t
33
CS3101-2, Lecture 5 STL Ceteris paribus, libraries are good Hard, subtle problems many mistakes don’t re-invent the wheel Unless we’re wheel artists better to commodify the wheel Use an “off-the-shelf” wheel like everyone else The standard wheel is reliable and efficient STL == Starbucks of programming Lots of important algorithms, data structures in CS Barring good reason use std versions
34
CS3101-2, Lecture 5 Standard Template Library Many template classes, functions Abstract data types Three general categories: 1. Containers 2. Iterators 3. Algorithms Three kinds of containers: 1. Sequences 2. Associative 3. Adapted
35
CS3101-2, Lecture 5 STL: “first-class” containers Sequences: vector: Dynamic-array-backed const-time random-access const-time insert/delete at back deque: double-ended queue fast random-access - how? fast insert/delete at front and back list: doubly-linked list fast insert/delete anywhere Associative: set: non-sequential, unique multiset: non-sequential, non-unique map: maps from keys to unique values multimap: maps to non-unique values
36
CS3101-2, Lecture 5 STL: containers Container adapters: use “first-class” containers by composition stack: LIFO queue: FIFO priority_queue Near-containers: arrays string bitset valarray
37
CS3101-2, Lecture 5 Container member ops & ftns copy constructor empty() size() swap First-class: begin() end() rbegin() rend() erase clear() NB: These are allow very simple - little more than getters
38
CS3101-2, Lecture 5 STL Iterators Standard way to traverse through container: iteration Abstraction of both “index” and “pointer” just: means of iterating forward, back, etc. Iterator direction types: 1. Forward iterator 2. Reverse iterator both supported by vector, list, etc. 3. Random-access iterator supported by vector Also: its can be const or not
39
CS3101-2, Lecture 5 Types of iterators I/O iterators are “one-pass” can only move in one direction can only traverse once – p++ Other types: bidirectional: p++, p-- random-access: p + i, p - i, p[i] *(p+i), p1 < p2 vector: random-access deque: random-access list: bidirectional set/multiset: bidirectional map/multimap: bidirectional
40
CS3101-2, Lecture 5 vector class Most commonly used container class Fast random access random-access iterators Can access mems with [] s like arrays – unsafe with at(i) – checks bounds, throws exception – safer Essentially: dynamic array hidden in obj add to/delete from back: const time unless run out of space autoly copy to larger array insert/del from middle: linear time must move half of mems forward/back
41
CS3101-2, Lecture 5 Vectors Similar to Java’s Vector in that: dynamic-array-backed list same complexities Different in that: takes insts of specified type vector nums; vector vals(20); size-20 vector of doubles vector objs; takes Base objects vector ptrs; takes Base* s or Extended* s
42
CS3101-2, Lecture 5 Template errors can be illegible Consider this ftn: template void printReverse(const vector &vect) { for (vector ::reverse_iterator curr = vect.rbegin(); curr != vect.rend(); curr++) cout << *curr << ","; } Slightly different from before how?
43
CS3101-2, Lecture 5 Template errors can be illegible When compiled: Error E2034 c:\Borland\Bcc55\include\rw/iterator.h 442: Cannot convert 'const int *' to 'int *' in function reverse_iterator ::reverse_iterator(const reverse_iterator &) Error E2094 vect.cpp 19: 'operator!=' not implemented intype 'reverse_iterator ' for arguments of type 'reverse_iterator ' in function printReverse (const vector > &) Error E2034 c:\Borland\Bcc55\include\rw/iterator.h 442: Cannot convert 'const int *' to 'int *' in function reverse_iterator ::reverse_iterator(const reverse_iterator &) Warning W8057 c:\Borland\Bcc55\include\rw/iterator.h 442: Parameter 'x' is never used in function reverse_iterator ::reverse_iterator(const reverse_iterator &) *** 3 errors in Compile *** Why? reverse_iterator not const_reverse_iterator
44
CS3101-2, Lecture 5 Vectors e.g. – vect.cpp template ostream& op &vect) { out << "("; for (int i = 0; i < vect.size(); i++) out << vect[i] << ","; out << ")"; return out; } template void printReverse(const vector &vect) { cout << "("; for (vector ::const_reverse_iterator curr = vect.rbegin(); curr != vect.rend(); curr++) cout << *curr << ","; cout << ")"; }
45
CS3101-2, Lecture 5 Vectors e.g. – vect.cpp void main() { srand(time(NULL)); vector ints; cout << "Initial size == " << ints.size() << "\nInitial capacity == " << ints.capacity(); for (int i = 0; i < 5; i++) ints.push_back(rand() % 20); cout << "\nNow, size == " << ints.size() << "\nCapacity == " << ints.capacity(); cout << "\nvector: " << ints; cout << "\nvector reversed: "; printReverse(ints);
46
CS3101-2, Lecture 5 Vectors e.g. – vect.cpp try { ints.at(100) = 20; } catch (out_of_range oor) { cout << "\nTried to set mem 100,"; cout << "\nbut caught exception: " << oor.what(); } sort(ints.begin(), ints.end()); cout << "\nAfter sort, vect: “ << ints; }}
47
CS3101-2, Lecture 5 Vectors e.g. – vect.cpp Initial size == 0 Initial capacity == 0 Now, size == 5 Capacity == 256 vector: (7,3,16,14,17,) vector reversed: (17,14,16,3,7,) Tried to set mem 100 to 20, but caught exception: index out of range in function: vector:: at(size_t) index: 100 is greater than max_index: 5 After sort, vector: (3,7,14,16,17,)
48
CS3101-2, Lecture 5 STL interators – iterio.cpp Access set of values from one place Usually, place is a container But: input stream may be construed as a place #include #include using namespace std; void main() { cout << “Enter two nums: “; istream_iterator intIn(cin); int x = *intIn; intIn++; x += *intIn; ostream_iterator intOut(cout); cout << “The sum is: “; *intOut = x; cout << endl; }
49
CS3101-2, Lecture 5 I/O iterators – ioiter.cpp Code: int x = *intIn; intIn++; x += *intIn; Output: C:\3101-2\lec5>ioiter Enter two nums: 5 6 The sum is: 11 But if code: int x = *intIn; /*intIn++;*/ x += *intIn; Then output: C:\3101-2\lec5>ioiter Enter two nums: 5 6 The sum is: 10
50
CS3101-2, Lecture 5 copy function – vect2.cpp Another way to print container: use copy function if (!vect.empty()) { ostream_iterator out(cout, " "); copy(vect.begin(), vect.end(), out); } copy(src begin it, src end it, dest it); src begin it: vect.begin() src end it: vect.end() dest it: ostream_iterator out(cout, " ") it’s an ostream_iterator it’s wrapping around cout it’s outputting T s it’s printing “ “ between the T s
51
CS3101-2, Lecture 5 shuffle, sort, search, min – vect2.cpp void sort(begin it, end it) it-s must be random-access members must support ==, < void random_shuffle(begin it, end it) same req’s bool binary_search(begin, end, target) same req’s also: assumes sorted min_element(v.begin(), v.end()) returns iterator
52
CS3101-2, Lecture 5 shuffle, sort, search, min – vect3.cpp All ftns translate automatically to strings transform ftn – vect4.cpp transform(begin it, end it, dest it, ftn) transform(v.begin(), v.end(), v.begin(), square); cout << "\nAfter squaring, vector: " << v << endl;
53
CS3101-2, Lecture 5 for_each ftn – vect4.cpp Another way to print container: use for_each function for_each(begin it, end it, ftn) Our subroutine: template void print(T val) { cout << val << "/"; } if (!vect.empty()) { for_each(vect.begin(), vect.end(), print ); } NB: print is a function pointer
54
CS3101-2, Lecture 5 Other containers list: doubly linked list insert/delete anywhere: const time access: linear time bidirectional iterators deque: double-ended queue insert/delete at front/back: const time insert/delete in middle: linear time access: constant time random-access iterators
55
CS3101-2, Lecture 5 strings as containers Can traverse strings in the usual way: for (int i = 0; i < i.length(); i++) cout << s[i]; Also: for (char::iterator curr = s.begin(); curr != s.end(); curr++) cout << *curr;
56
CS3101-2, Lecture 5 STL Algorithms - binary_search sort count: count(list.begin(), list:end(), val, num); equal: compares containers for_each: applies ftn to each element copy: copies container reverse min/max Some in
57
CS3101-2, Lecture 5 Other algorithms Set-theoretic: set_union set_intersection set_difference set_symmetric_difference Sorting: sort_heap stable_sort And many more…
58
CS3101-2, Lecture 5 Algorithms in STL Important observation: STL class live in own headers -,, but STL algs live in places like and To sort, we pass access (it) to our obj to the sort ftn We don’t call obj.sort() Why? The STL doesn’t use inheritance! Why not? virtual functions are slow(er) No inher would have to dup. ftns No inher no encapsulation algorithms on their own
59
CS3101-2, Lecture 5 STL e.g.: grades Goal: store grades for group of students ordered set of assignments maybe students with same name For each student, have grades want fast access to each grade use vector of chars typedef vector Grades; First, create map of students: map roster;
60
CS3101-2, Lecture 5 STL: add a student to map map represents a function: key maps to value map, basically: set of ordered pairs pair template void Roster::addStudent(const string &name) { //check if already exists if (roster.find(name) != roster.end()) return; //check for room if (roster.size() == MAX) waitList.push_back(name); else { Grades grades; roster.insert( pair (name,grades)); }
61
CS3101-2, Lecture 5 STL: add a student to map Notice find line: if (rost.find(name) != rost.end()) return; find function searches for an elm with our key if found, returns pointer to it if not, returns pointer to end() points past, not to last member like: for (int i = 0; I < n; i++) More precisely: these ptrs are iterators – inc/dec to step through seq More precisely: these ptrs are iterators ops overloaded as though moving thru mem
62
CS3101-2, Lecture 5 STL: add a student to map Notice insert line: roster.insert( pair (name,grades)); Dissection: Add a member to a roster The member is a pair of two things member ftns: first(), second() The things are string and Grades pair (x,y) is a constr call: passes x and y to constr for type pair
63
CS3101-2, Lecture 5 STL: drop a student void Roster::dropStudent(String &name) { if (roster.find(name) == roster.end()) return; roster.erase(name); if (waitList.size() > 0) { string wait = waitList.pop_front(); waitList.pop(); Grades grades; roster.insert( pair (name,grades)); } }
64
CS3101-2, Lecture 5 STL: set a grade void Roster::setGrade(const &string name, const int assign, const char grade) { map stud = roster.find(name); if (stud == roster.end()) { cerr << “not found\n”; return; } if (stud->second().size() <= assign) stud->second().resize(assign+1); stud->second[assign] = grade; }
65
CS3101-2, Lecture 5 STL: print grades void Roster::print() { … } Already saw many print/<< functions… That’s all we’ll cover of STL Many more classes, algs in STL Much more to C++ itself But: you now know enough About the most import. Features To learn remaining details on own
66
CS3101-2, Lecture 5 Next time: final exam Closed books/notes/everything 2 hours/class time Questions: “Vocab”: protected, static, etc. Find errors/read code/predict output Write code See web for a semi-definitive list of topics Jake will be proctoring the exams But we’ll have OH next week(end) Come in if you have questions!
67
CS3101-2, Lecture 5 Grading Final will be hard/challenging Ave score probably near 70% Final grades are curved Rule of thumb: mean ~ stdev Mean ~ B/B- +-stdev ~ one letter grade See mean/stdevs on web to estimate your grade
68
CS3101-2, Lecture 5 The future Q: What happens when backward-compat is removed from C++? A: C# less complexity Microsoft approves Better or worse than C++, Java? Find out in CS3101-4: C#, next spring Tonight: hw4 TBA – due by final Please fill out course evals! Link will be on classpage tonight Available until beginning of finals Get valuable extra credit on final Sign in and good luck! Happy Thanksgiving!
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.