Brief notes on C++ formatting and using std::vector

// there is no standard for C++ file extension
// people variously use: .C, .cc, .cxx, .cpp, .c++
// and similarly .H, .hh, etc. for header files (no no suffix, see below)
// I prefer .cc and .hh

// compile with:
// g++ -std=c++1z -Wall -Werror -fno-exceptions -fo-rtti -o vectortut

// c++1z refers to C++17 which was just ratified (this month, in December, 2017)

// the -fno- options turn off exceptions and run-time type information (rtti)
// On this point I agree with the LLVM coding standards, see:

// standard C++ headers typical lack a suffix
#include <vector>
// is the best online C++ reference guide.  See:
// C++ versions of standard C headers typically start with c
#include <cassert>
#include <cstdio>

main() {
    // declare an (empty) vector of int
    // std is a namespace
    // std::vector refers to the vector in the namespace std
    // template (type) parameters are passed in angle brackets: <>
    // like Array[Int] in Scala
    std::vector<int> v;

    // verify v is empty
    assert(v.size() == 0);

    // add some elements to the end of v
    assert(v.size() == 3);

    // you can access elements of v operator []
    assert(v[1] == -2);
    // at is alias for operator []
    assert( == 5);

    int sum = 0;
    // C++ supports C-style for as well as range-based for
    for (int i : v) {
        sum += i;
    assert(sum == 10);

    // set v back to empty

    printf("tests passed.\n");

    return 0;