Commit 11756238 authored by Davis King's avatar Davis King

Added the csv io manipulator that lets you print a matrix in cvs format.

parent a97b5794
......@@ -1813,6 +1813,51 @@ namespace dlib
This function is defined inside the matrix_read_from_istream.h file.
*/
// ----------------------------------------------------------------------------------------
class print_matrix_as_csv_helper
{
/*!
This object is used to define an io manipulator for matrix expressions.
In particular, this code allows you to write statements like:
cout << csv << yourmatrix;
and have it print the matrix with commas separating each element.
!*/
public:
print_matrix_as_csv_helper (std::ostream& out_) : out(out_) {}
template <typename EXP>
std::ostream& operator<< (
const matrix_exp<EXP>& m
)
{
for (long r = 0; r < m.nr(); ++r)
{
for (long c = 0; c < m.nc(); ++c)
{
if (c+1 == m.nc())
out << m(r,c) << "\n";
else
out << m(r,c) << ", ";
}
}
return out;
}
private:
std::ostream& out;
};
class print_matrix_as_csv {};
const print_matrix_as_csv csv;
inline print_matrix_as_csv_helper operator<< (
std::ostream& out,
const print_matrix_as_csv&
)
{
return print_matrix_as_csv_helper(out);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment