240 likes | 367 Views
Software Engineering 3156. 28-Nov-01 #23: OS, Language, Design Patterns Phil Gross. Administrivia. Integration prototype is up JSP and Servlets Wednesday, Nov 28 th , 6-8pm Final exam Very little C++/OS A bit of C Research Fair this Friday. More on C. Will be on next homework
E N D
Software Engineering 3156 28-Nov-01 #23: OS, Language, Design Patterns Phil Gross
Administrivia • Integration prototype is up • JSP and Servlets • Wednesday, Nov 28th, 6-8pm • Final exam • Very little C++/OS • A bit of C • Research Fair this Friday 2
More on C • Will be on next homework • Need to be able to do basic stuff • Simple C++ is generally easier than C • Although C++ is capable of being much more complicated 3
Miscellany on structs • Since not everything happens in main, malloc’ing structs is very common • The sizeof operator works on pretty much any datatype, be it structs or primitive datatypes • Guaranteed tip: you will come across PointP myP = (PointP)malloc(sizeof(PointP)); • and be glad that you were attending this class… 4
Miscellany on structs • (*myP).x is messy • C has a shortcut: myP -> x • Always remember that a -> b is equivalent to (*a).b • Precedence of operators • It’s cumbersome to have to say struct Point every time • Use typedefs: define “aliases” typedef struct Point PointT; • or even typedef struct Point *PointP; 5
Function prototypes • As I said, C’s compiler is not all that smart • If you have: int main(void) { int i = foo(); } int foo(void) { return 5; } • It will not work 6
Function prototypes (II) • Need to clue in the compiler by putting the following prototype before main: int foo(void); • Looks like a Java interface construct • Often, you collect the prototypes into a header (.h) file, then #include it into your various source (.c) files 7
On various .c files… • A common way to build multi-source-file code in C is to • build a set of .h files that export functionality (i.e. “public” methods) • #include these .h files • Compile and link the .c files together • As long as you have a prototype, the compiler will not care about the definition • Like compiling against an Interface • The Linker will, though… 8
Useful libc commands (I) • printf: Output text: like System.out.println printf(“%d\n”, intval); printf(“%d%f\n”, intval, floatval); man printf • scanf: Input text: no BufferedReader scanf(“%d”, &intval) • note the ampersand • Also, fprintf/fscanf and sprintf/sscanf 9
Useful libc commands (II) • gets(string) • gets a line of text and puts it in string • Janak says: Should technically use fgets(stdin, string) • Phil says: Never use gets(). Or if you do, never complain about Microsoft bugs again. • Why not &string? • puts() also exists: more like println, actually • Can’t use + to concatenate 10
Streams in C • FILEs (almost always FILE *) • Open with fopen(), close with fclose() • Have fread() and fwrite() • Plus fprintf() and fscanf() • And ftell()/fseek() for random access • Are buffered • Have pushback 11
Text to int • atoi(string) • Roughly equivalent to Integer.parseInt, although not nearly as smart • No exceptions • Can also use sscanf() • Slower, but much more programmable 12
STL Again • Really quite cool • Often can do away with most explicit loops • Which is a good thing 13
The Idea • How to iterate through an array • Method 1: by index • Method 2: using pointers • See stl_1.cc • Next step is obviously templates • See stl_2.cc 14
What About Vectors? • Need something “pointer-like” for vectors • Has to support *, ++, ==, != • Plus some way of getting begin and end values • All Standard Library containers (including vectors) support this! • “Pointer-Like-Thingies” are called Iterators • Defined inside the container class 15
A Note On Scope • Scope = where a variable is visible • C++ allows you to explicitly specify scope using the :: operator • foo::bar • bar is defined inside class or namespace foo • ::baz • baz is global • See scope.cc 16
What Do Iterators Look like? vector<string> svec; vector<string>::iterator iter = svec.begin(); • What is the type of iter? • Answer: “the iterator type defined inside class vector<string>” • All container types have functions begin() and end() that return iterators 17
Sooo…. • Suppose we templatize our find() function on both elemType and iterType • Now it works on arrays • And all containers in the standard library • See stl_3.cc 18
What are These Function Objects? • find_if() takes a predicate function • Returns values for which it is true • How do we do that, exactly? • Naïve filter implementation: stl_4.cc • Function pointer implementation: stl_5.cc • Function objects: zero overhead function-pointer-like thingies supplied with STL 19
Such As? • Arithmetic • plus<t>, minus<t>, negate<t>, multiplies<t>, divides<t>, modulus<t> • Relational • less<t>, less_equal<t>, greater<t>, greater_equal<t>, equal_to<t>, not_equal_to<t> • Logical • logical_and<t>, logical_or<t>, logical_not<t> 20
Example • Create a new sequence that is the sum of fib and pell transform(fib.begin(), fib.end(), pell.begin(), target.begin(), plus<int>() ); 21
Seems Kind of Limited • Function adapters add flexibility • Bind1st and bind2nd fix the values of the first or second parameters of a binary function • find_if(iter, vec.end(), bind2nd(less<int>, 10)) • Finds first item that is less than 10 • You can make your own function objects 22
Maps and Sets map<string, int> words; words[“Vermeer”] = 7; if (words[“Rembrandt”] == 3) {… set<string> modern; if (modern.count(“Mondriaan”)) {… 23
What If Our Target is Initially Empty? • Iterator inserters • Act like output iterators, but actually call insertion methods • back_inserter(C), front_inserter(C), inserter(C,i) unique_copy(ivec.begin(), ivec.end(), back_inserter(result_vec)); 24