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

// vectortut.cc
// 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 vectortut.cc -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:
// https://llvm.org/docs/CodingStandards.html#do-not-use-rtti-or-exceptions

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

int
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.empty());
    assert(v.size() == 0);

    // add some elements to the end of v
    v.push_back(5);
    v.push_back(-2);
    v.push_back(7);
    assert(v.size() == 3);

    // you can access elements of v operator []
    assert(v[1] == -2);
    // at is alias for operator []
    assert(v.at(0) == 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
    v.clear();
    assert(v.empty());

    printf("tests passed.\n");

    return 0;
}