C++

"If you're not at all interested in performance, shouldn't you be in the Python room down the hall?"
  -Scott Meyers

Synopsis

Hi, my name is Clayton.  I'm documenting my journey towards learning the C++ language.  This website is a repository of subjectively interesting C++ topics, books, and references.  My end-goal is to enable myself (and help others along the way) to effectively harness the performance benefits of C++. 


Projects


Algorithms + Data Structures

To use C++ effectively, a basic understanding of algorithms and data structures is required due to the flexibility allowed via STL Collections.  Performance can be improved or impaired based on the combination of both the algorithm and the data structure used by the algorithm.  The two go hand-in-hand, each algorithm + data structure has specific strengths + weaknesses and should be carefully choosen for optimal function.  Choose the data structure which results in the simplest code which performs the most common use-case operations efficiently.


C++ Programming Courses

University of Washington (www.pce.uw.edu)


Books

Introductory

Andrei Alexandrescu

Herb Sutter

Scott Meyers

Supplemental

Specialization

Interview Practice

The following books are subjectively ordered top-down from "easy" to "difficult"


References

Standard Template Library (STL)

STL Algorithms

  Idioms

STL Collections (Containers / Data Structures)

  Type   Examples   Strength   Weakness
Sequence Collections
  • std::vector - random access, add/remove from back
  • std::string - random access, add/remove from back
  • std::deque - random access, add/remove from front/back
  • std::list - add/remove from any position
  • std::vector - add/remove from beginning/middle
  • std::string - add/remove from beginning/middle
  • std::deque - add/remove from middle
  • std::list - bi-directional iterator access ( no random-access )
Associative Collections     Ordered:     Unordered:
  • usually implemented as balanced binary tree, thus O(logN) time for insert, find, and remove
  • find element >= x: lower_bound(x)
  • find element > x: upper_bound(x)
  • find element(s) == x: equal_range(x)




  • implemented as hash tables, thus O(1) time for insert, find, and remove
  • elements cannot be directly updated due to ordered constraint [i.e. remove() is not allowed], instead use erase() and insert() for "updates".
  • elements cannot be directly accessed, instead elements are indirectly accessed via iterators [note: std::map::at() and std::map::operator[] return a reference to the key's value]
  • bi-directional iterator support, thus cannot be used with algorithms requiring random-access iterators




  • trade-off between O(N) space and O(1) time
Collection Adaptors
  • specialized usage
  • generalized usage

STL Iterators

Guidelines for iterators

Choose an iterator with the least amount of capabilities which are sufficient for a particular use-case needs in order to keep code generic and less error-prone.  For example, the input iterator is the proper iterator to use for console input.

  Type  Capability
Input iterator
  • single increment
  • dereference
Output iterator
  • single increment
  • assignment
Forward iterator
  • all Input iterator capabilities
  • all Output iterator capabilities
Bidirectional iterator
  • all Forward iterator capabilities
  • single decrement
Random Access iterator
  • all Bidirectional iterator capabilities
  • increment by more than one
  • decrement by more than one

Memory

Smart Pointers         More Info and Example Usage     

L values + R values

L values R values
  • multi-statement duration
  • has an address (long-lived location in memory)
  • single-statement duration
  • typically does not have an address (ephemeral)

std::move + std::forward


Header file ordering

Header files should not be dependent upon other header files being included first.  Each file should include what it needs.  In general use the following order for includes to avoid include-order dependencies:

  1. user
  2. third party
  3. system

C and C++ interworking

Conversions

Macros

This section is intended as a mapping between C style and C++ style code snippets.  In general, avoid C style macros and prefer C++ classes instead.

C style C++ style
  • INT_MIN
  • INT_MAX

Preprocessor

Other


Additional References


Modern C++ Overview


Ancillary Quotes

"There are two fundamentally different ways to approach the matter: the way most people do it and the right way."
  -Scott Meyers
"C++ is my favorite garbage collected language because it generates so little garbage."
  -Bjarne Stroustrup
"Knowing it will score you mondo points with those few souls who care."
  -Scott Meyers

Programmer's Log


Contact Me

claytonjwong@gmail.com