10 C++ tutorial
10.0.1 I know R but not C++
Summary of how syntax differs between R and C++:
R code C++/TMB code
# // // Comment symbol
Comments 3.4 Type(3.4); // Explicit casting recommended in TMB
Constants x = 5.2 Type x = Type(5.2); // Variables must have type
Scalar x = numeric(10) vector<Type> x(10); // C++ code here does NOT initialize to 0
Arrays x[1]+x[10] x(0)+x(9); // C++ indexing is zero-based
Indexing for(i in 1:10) for(int i=1;i<=10;i++) // Integer i must be declared in C++
Loops x[1] = x[1] + 3 x(0) += 3.0; // += -= *= /= incremental operators in C++ Increments
It is important to note the following difference compared to R:
Vectors, matrices and arrays are not zero-initialized in C++.
A zero initialized object is created using Eigens setZero()
:
matrix<Type> m(4,5);
m.setZero();