C++ basic salient points that I made mistakes on
- int main(int argv, char* argc[]), argc[0] is always the program's name that you run
- switch() statement doesn't accept std::string. One way to solve this is to use enum, see here: link
- "player".size() won't work! std::srting("player").size() will work !!!!
- int i = j = 0 is wrong if j is not defined before. A correct way is to define them separately.
- 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 !- hour, minue & second cannot be negative
- single digital need to add a 0 before it
- 12 o'clock at noon is PM
- hour shouldn't be bigger than 24, minute & second shouldn't be bigger than 60
- minute and second should be 0 when hour = 24
- when output 24:0:0, it should be 12:0:0 am
So need to be careful for the corner cases !
- always pass ostream and istream object by reference. Otherwise will cause problem "attempting to reference a deleted function" problem. See this link
- 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 - 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_;} - 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 ....
- 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.
- template class declaration:
template <class T> class Node{ };