Basic knowledge C/C++ (2)
- Difference between C++ and java
- Java runs in a virtual machine
- C++ supports unsigned arithmetic
- C++ allows operator overriding
- C++ àmultiple inheritance of class
- Java has a build-in garbage collection
- In java, parameters are always passed by value
C++ value, pointer or by reference
- Hash table // value stored by applying hash function on a key, additionally, since hash tables use the key to find the index that will store the value, in insert/lookup can be done in a mortised O(1) time, we should handle potential collisions (use the hash function to generate the hash value, but ) in hashtable:
- mod prime number to ensure that the hash value is unique,
- a collision resolving method // chaining probing
- implement methods to dynamically increase or decrease the hash table size
- open addressingà retrieving separate chaining// (H(key)+di) mod M
- deep copy and shallow copy
deep: copy all fields and make copies of dynamically allocated memory// overload the assignment operator
shallow: (default copy constructor) may cause a lot of programming run-time errors especially with the creation/ deletion of objects. It may pass information about a complex structure without duplication of data. Careful with destruction of shallow copy
struct Test {char * ptr; }
void shallow_copy (Test &scr, Test &dest )
{ dest.ptr=src.ptr}
void deep_copy (Test &scr, Test &dest)
{dest.ptr= malloc (src.ptr+1);
memcpy(dest.ptr, src.ptr);}
- map: use red-black-tree as data structure, so the elements you put in there are sorted and insert/delete is O(lgn) à better in fewer data
hash map: use a has unsorted insert/delete O(1) à better in lots of data
- destructor in base class should be virtual (these allocated resources must be deallocated before the object id destroyed)
deallocate memory clean up for a class object. A destructor is called when that object passes out of scope or be deleted
no argument/ no return type
- smart pointer: avoids dangling pointers, memory linkage allocation failure, garbage collection
- private constructor: make the class can’t be inherited; on one out of the class can instantiated the class
- STL: Standard Template Library
#include <file.h>àSTL
#include “file.h”àin the files
- Vector is the type of sequence that should be used by default
Vetor <type> name; size cannot be changed vetor <int> I;I[10] è better for store/retrieve not good for insert/ delete
List: we cannot use [] to retrieve elements à better for insert/ delete, not good for store/ retrieve
- Templat : Special function that can operate with generic types can be adapted to more than one type or class help as reduce the code