C++ basic salient points that I made mistakes on

  1. int main(int argv, char* argc[]), argc[0] is always the program's name that you run
  2. switch() statement doesn't accept std::string. One way to solve this is to use enum, see here: link
  3. "player".size() won't work! std::srting("player").size() will work !!!!
  4. int i = j = 0 is wrong if j is not defined before. A correct way is to define them separately.
  5. corner cases !!!
    problem: given two timestamps 2:30:4 and 14:03:31, output them like this: 02:30:04 AM and 2:03:31PM. 
    This is very simple question, but you need to think carefully, as there are so many corner cases !
    1. hour, minue & second cannot be negative
    2. single digital need to add a 0 before it
    3. 12 o'clock at noon is PM
    4. hour shouldn't be bigger than 24, minute & second shouldn't be bigger than 60
    5. minute and second should be 0 when hour = 24
    6. when output 24:0:0, it should be 12:0:0 am

    So need to be careful for the corner cases !

  1. always pass ostream and istream object by reference. Otherwise will cause problem "attempting to reference a deleted function" problem. See this link
  2. when iterating a const vector/list, always to use const_iterator:
    std::list<Recipe>::const_iterator itr
    const_iterator can only call const member functions
  3. when returning a list/vector, remember to return its reference. Usually, if you don't want to change its value, return a const reference. DO NOT RETURN VALUE !
    eg: const std::list<Ingredient> & get_ingredients() const {return this->ingredients_;}
  4. int main() { return 0;} return 0 means successful... (I know it's trivial, till one day I couldn't decide whether it's return 0 or return 1 ....
  5. delete operator deallocates the allocated memory. It's not going to erase the pointer variable on the stack. It (only) cleans the memory on the stack. The pointer variable's value doesn't change. 
    example: 
    int* x = new int(8);
    std::cout << x << std::endl; // output 0x4440250
    delete x;
    std::cout << x << std::endl; // output 0x4440250

    so that you can see the value of point x is the same before and after delete operation. However, an error will occur if you try to dereference x after delete. 

  6. template class declaration: 
    template <class T> class Node{ };
posted @ 2016-02-02 05:15  Rui Yan  阅读(141)  评论(0编辑  收藏  举报