There's not a lot left after the characteristically excellent answer from Morwenn, but I noticed a few things that may also help.
First, I see a lot right with this code. You didn't use using namespace std;
, your functions are actually documented with comments and the code generally seems well though-out and complete. Good for you!
Here are some things that could be improved:
Provide a matching #endif
This is probably just a cut-and-paste error, but there is no matching #endif
for the #ifndef MATRIX_H
at the top of the file.
Fix the return type for some functions
As the comment indicates, you already knew the return type wasn't quite right for a few of your methods:
// don't unfortunatly lnow what to replace auto with
auto Begin_col( std::size_t i ){
I believe that the answer is that it's a boost::range_iterator<T>::type
typename boost::range_iterator<T>::type Begin_col( std::size_t i ){
However, with that said, it might be worth considering whether this is really the type you want to use for this.
Fix operator<<
I supplied the missing Vector.h
file and then used this test code:
int main()
{
Matrix<std::string, 2, 3> m{
"Larry", "Moe", "Curly",
"Belgium", "Netherlands", "Luxembourg"
};
std::cout << m << '\n';
}
This fails because of the code in the operator<<
method:
std::ostream& operator<<(std::ostream &os, const Matrix<T, row_dim, col_dim>& m){
// uh oh... this assumes that the elements are numeric and
// can be converted to double
double max = *std::max_element( m.Cbegin(), m.Cend() );
I see what you're trying to do there, setting up the width so that all of the columns are aligned, but the method you're using isn't quite right. One way to do this would be to find the maximum width of all elements as printed and then use that to set the column width.
Instead, you could use this:
const size_t colwidth = std::accumulate(m.Cbegin(), m.Cend(),
0, getPrintedLength<T>);
The helper function is then defined like this:
template<class T>
size_t getPrintedLength(size_t currmax, const T &thing)
{
std::ostringstream in;
in << thing;
return std::max(in.str().size(), currmax);
}
Now the column sizing works for any type for which std::ostream& operator<<
is defined.
Separate prompt from input in istream operator>>
Right now, the istream operator>>
function prints a prompt and asks for \$n\$ numbers, where \$n\$ is the number of elements in the Matrix
. However, as shown above, they aren't necessarily numbers. Or, if they are numbers, they might be complex numbers.
Consider changing the Begin_col
and End_col
iterators
What happens if I iterate from m.Begin_col(0)
to m.End_col(1)
? I suspect it's not going to work as you expect, but I could be wrong since the functionality depends on the missing Vector.h
code.