A pointer to a matrix data array can be obtained using the member function .data() .
The size of the data array can also be obtained using the member function .size() .
Using these two, we now have pointers to the first element and the end of the array as a.data() and a.data()+a.size() .
In addition, we know that std::vector can be initialized using iterators (or array pointers in our case).
Thus, we can get a double vector that wraps matrix elements with std::vector<double>(a.data(), a.data()+a.size()) .
This vector can be used with loop syntax for a loop that is included in your code snippet, like:
Matrix2d a; a << 1, 2, 3, 4; double sum = 0.0; for(double d : std::vector<double>(a.data(), a.data()+a.size())) { sum += d; }
source share