Saturday, December 12, 2015

Book reading: a tour of C++

Dec. 12, 2015

  Enjoy 2 hours to read the book: tour of C++ on Saturday morning.

  A Tour of C++
  http://www.amazon.com/A-Tour-C-In-Depth/dp/0321958314

  Plan to spend 10 hours to read the book.

  First 3 hours reading, here are favorite advice from the book:
  the chapter 9 - containers  Page (104-105)
  Advice:
  1. Use vector as your default container
  8. User push_back() or resize() on  a container rather than realloc on an array
  13. To preserve polymorphic behavior of elements, store pointers (Julia's rating: A)
  14. Insertion operators, such as insert() and push_back() are often surprisingly efficient on a vector.
  17. A map is usually implemented as a red-black tree.
  18. An unordered_map is a hash table.
  19. Pass a container by reference and return a container by value (Julia: look into this advice more)
  20. For a container, use the ()-initializer syntax for sizes and the {}-initializer syntas for elements.
  21. Prefer compact and contiguous data structure (Julia's rating: A)
  22. A list is relatively expensive to traverse.
  23. Use unordered containers if you need fast lookup for large amounts of data.
  24. User ordered associative containers (e.g., map and set) if you need to iterate over their elements in order
  25. Use unorderd containers for element types with no natural order.
  28. Know your standard-library containers and prefer them to hand-crafted data structures.

 When Julia works on leetcode algorithms question early in 2015, she found out that most talent programmers tend to use C++ to write solution. Now, she is determined to finish this book reading: a tour of C++, she likes to master C++ some day.

  Further reading on the advice 13 (Dec. 12, 2015):
http://stackoverflow.com/questions/141337/c-stl-should-i-store-entire-objects-or-pointers-to-objects

http://stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself?rq=1    (Julia enjoys reading the blog, she spends over 30 minutes on this blog.)
Notes from the above stackoverflow blogs:
Dynamic allocation

You need the object to outlive the current scope
You need to allocate a lot of memory

Pointers

You need reference semantics
You need polymorphism
You want to represent that an object is optional
You want to decouple compilation units to improve compilation time
You need to interface with a C library

Polymorphic behavior
Reference semantics and avoiding copying
Resource acquisition
More fine-grained life-time control

http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap


No comments:

Post a Comment