Overblog Tous les blogs Top blogs Technologie & Science Tous les blogs Technologie & Science
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU

Basic knowledge C/C++ (2)

15 Mars 2013 Publié dans #c-c++

  1. 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

  1. 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:
    1. mod prime number to ensure that the hash value is unique,
    2. a collision resolving method // chaining probing
    3. implement methods to dynamically increase or decrease the hash table size
    4. open addressingà retrieving separate chaining// (H(key)+di) mod M
  2. 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);}

  1. 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

  1. 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

  1. smart pointer: avoids dangling pointers, memory linkage allocation failure, garbage collection
  2. private constructor: make the class can’t be inherited; on one out of the class can instantiated the class
  3. STL: Standard Template Library

#include <file.h>àSTL

#include “file.h”àin the files

  1. 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

  1. Templat : Special function that can operate with generic types can be adapted to more than one type or class help as reduce the code
Publicité
Partager cet article
Repost0
Pour être informé des derniers articles, inscrivez vous :
Commenter cet article