CS62 - Spring 2010 - Lecture 19
Quiz: Exercise 15.3
Suppose you have a hash table with seven entries (indexed 0 through
6). This table uses open addressing with the hash function that maps each letter to its alphabet code (a = A = 0, etc.) modulo 7. Rehashing is accomplished using linear-probing with a jump of 1. Describe the state of the table after the letters D, a, d, H, a, and h are added to the table.
hashtable size
- some rules of thumb
- ~half full (default for java is .75,
http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html
) for open addressed hashtables
- why?
- recall, running time for most methods is O(1/1-alpha))
- alpha = .75 gives us O(4), but degrades rapidly after that
- prime number sizing
- what happens if the size of the hashtable is a power of 2, e.g. 2^p?
- if, like we did, we take the hashCode() and mod it with the hashtable size, what are we getting?
- moding by 2^p maps the number so the numbers 0... 2^p
- these are represented by the lower order p bits
- not a very good use since we throw away the upper order bits
- what happens if the load of our hashtable gets too large?
- similar to ArrayList, create a new table that's larger
- can we just copy the values over?
- no, need to rehash since we've changed the size of the table, e.g. if use used mod m, then entries would be wrong
look at
first.cpp code
- our first C++ program!
- what are some things that you notice that are common to Java?
- main method
- like Java, execution of the code starts in the main method
- int variables
- C++ has many of the standard types we've seen: int, double, float, char
- and a few ones that are slightly different:
- "long" in Java is "long int" in C++
- "boolean" in Java is "bool" in C++
- while we're on it, Java has 8 built in types:
- short, int, long, double, float, boolean, char, byte
- similar method construction
- return type
- method name
- parameters
- curly braces
- control constructs
- for loop
- if statement
- C++ has all the other familiar ones as well:
- while
- do-while
- switch
- if/else
- method invocation
- method name followed by parenthesis, which enclose the actual parameters
- class method invocation with '.'
- vector class
- use of generics with the vector class, i.e. <int>
- C++ has had generics for a while
- Java just got them for version 1.5, which came out a few years ago (the latest version is v1.6)
- C++ can be challenging to get right sometimes, but it should be noted many similarities there are between Java and C++
- what are the differences?
- where is the class???
- unlike java, C++ code does not need to be in a class
- in fact, although we will use multiple files to make our life easier, you can think of it as one gigantic chunk of code
- main method
- returns an int rather than being of type void
- don't forget to return a value
- a value of 0 means everything went ok
- any other value, reports an error
- doesn't have all that other stuff... public static
- why did it need to be static in Java?
- methods have to be inside a class
- but to call a non-static method, we need an instance of the class (chicken and the egg :)
- since it's not in a class, scope (public) isn't needed
- doesn't take any arguments
- though we can have a version that does take command-line arguments, but that's for another day
- #include???
- C++ can be thought of as one giant file of code (eventually)
- if we need to use code from another class, we need to use #include
- anything starting with # is called a pre-processor directective
- the preprocessor runs before the compiler does
- it can manipulate files, etc.
- #include <x> says "copy the code from 'x' at the beginning of this file"
- <> denotes a built-in, system class
- we can use quotes "" to denote a user class/file and give the path to the file
- using namespace std
- what does the import command do in Java?
- the import command makes our life easier
- let's say we want to use the ArrayList class
- we could specify the full package path and use it that way:
java.util.ArrayList list = new java.util.ArrayList();
- this gets old pretty quickly
- if we import java.util.Arraylist, that says to make ArrayList part of our namespace, that is, whenever we refer to ArrayList, we're actually referring to java.util.ArrayList
- in C++, the "using namespace std" commands tells the compiler to allow everything in the "std" namespace to be referred to without having to do anything special
- equivalent of saying "import std" or something similar in Java
- in C++, to refer to something in a namespace you type the name of the namespace followed by "::", i.e. "std::"
- the vector class is in the std namespace, so it saves us from writing std::vector everywhere
- in general, for most of our programs, you'll want to have this there (don't forget!!!)
- vector
- vector, similar to the Java Vector (or ArrayList) class
- what happened to the "get" and "set" methods of Java?
- in C++, you can access vector elements using the "[]"
- in fact, there is no get or set method in the sense that Java has
- why does this work?
- one of the neat things you can do in C++ is "operator overloading"
- what is an operator?
- special symbols that perform specific operations
- what operators have we seen?
- +, -, ...
- =, +=, ...
- ==, !=, <=
- &, |, ^
- <<, >>
- ++, --
- ?
- in C++, a class can change the functionality of built-in operators, e.g. '=', '<<' or, in this case '[]'
- Java can't do this, which is why we need methods like "equals"
- it looks like in the simpleVersion method we're just creating a new variable to hold a vector, but this actually creates a vector!
- "vector<int> nums" calls the zero parameter constructor in the vector class
- how do we call constructors with parameters?
- "vector<int> nums(10)" call the constructor with one parameter in the vector class
- be careful!
- "vector<int> nums()" does NOT call the zero parameter constructor
- you'll get a compiler error
- vector class is sort of a blend of arrays and ArrayList class
- like noted above, we can use the '[]' operators on it to access items
- notice that when we call the one parameter constructor, we can access the elements
vector<int> nums(10);
nums[8] = 10;
- what would happen if we did this in Java?
- index out of bounds exception
- what do you think will happen if we do:
vector<int> nums;
nums[8] = 10;
run-time error (aka segmentation fault), just like Java, we cannot access elements in the vector that have not be instantiated (either via a push_back or via the constructor)
- vector is part of a number of data structures in the STL (Standard Template Library)
- look at C++ references on CS page
- look at the different STL library classes
- look at the vector class documentation
- many similar methods
- size
- insert
- some slightly renamed methods
- push_back instead of add
- there is an "at" method, but it's not the same as get... more on this later
- notice it has two things that start with "operator"
- operator[] (overloading of the [] for element access)
- operator= (overloading of the =)
- notice that it's listed as a constructor
- more on this in a bit
- what is this "cout" business?
- made available because of "#include <iostream>"
- cout is an outputstream (similar to streams in Java)
- cout is the particular name for the stream that allows you to print to the console/terminal
- again, look at C++ documentation
- C++ Standard Library: Input/Output Stream Library
- if we scroll down, you can see "cout"
- "cout is an object of class ostream that represents the standard output stream"
- notice that it's just an object
- look at ostream class
- what do you think we'll find in this class?
- "operator<<"
- "performs an output operation on that stream"
- for cout, that means printing to the console
- notice that it has methods for many different types
- notice that it returns an ostream
- how does that help us?
- allows us to chain multiple << together
- back to the code... what does "cout << val << endl" do?
- prints value of val to the console
- prints "endl" to the console?
- endl is a variable for the "end of line"
- it's included with <iostream>
- filename is first.cpp?
- what are the filename restrictions in Java?
- class A, must be in A.java
- in C++, there are no naming restrictions. You can use one file, multiple files and the filenames do not matter
- by convention, when you have a class definition, you should put it in a filename with that same name .cpp
- what does max do?
- finds the maximum value in the vector
- looks almost identical to what we would write in Java
- what does simpleVersion() do?
- creates a new vector
- how many elements in it initially?
- 0, it uses the default constructor
- adds 1, 2 and 3 to the vector
- calls max, to get the maximum value
- prints out the maximum value to the console
- in this case, 5
- what does moreInteresting() do?
- again, creates a new vector
- creates a second vector nums2
- sets the first entry in nums to be 15 (rather than 1)
- calls max on nums2
- what do you think is output?
- two options:
- 3
- 15
- what would be output in Java?
- 15
- 3 is actually output
- what does this mean? why do you think this happens?
- nums and nums2 are different objects