打印犰狳矢量/矩阵(C+;+;)后禁用换行 我使用C++和犰狳库。打印向量或矩阵时,即使使用.raw_print(),向量/矩阵后也始终包含换行符。有什么简单的方法可以使这种行为失效吗
最简单的例子:打印犰狳矢量/矩阵(C+;+;)后禁用换行 我使用C++和犰狳库。打印向量或矩阵时,即使使用.raw_print(),向量/矩阵后也始终包含换行符。有什么简单的方法可以使这种行为失效吗,c++,armadillo,C++,Armadillo,最简单的例子: #include <iostream> #include <armadillo> using namespace std; using namespace arma; int main() { rowvec a; a << 0 << 1 << 2; cout << a; a.print(); a.raw_print(); mat b; b <
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
int main() {
rowvec a;
a << 0 << 1 << 2;
cout << a;
a.print();
a.raw_print();
mat b;
b << 0 << 1 << 2 << endr
<< 3 << 4 << 5 << endr
<< 6 << 7 << 8;
cout << b;
b.print();
b.raw_print();
}
中的函数旨在执行“”。.raw_print()函数减少了漂亮打印的数量(即,它不会将数字的表示形式更改为科学格式),但仍会打印换行符
如果这些函数的功能更少,那么它们将不会提供任何附加值,只需通过元素循环并将其转储到用户流(例如cout)。因此,解决方案只是通过以下功能自己进行打印:
inline
void
my_print(const mat& X)
{
for(uword i=0; i < X.n_elem ++i) { cout << X(i) << ' '; }
}
内联
无效的
my_打印(const mat&X)
{
对于(uwordi=0;irowvec
类型固有的东西。在打印矩阵时,这一点也很明显,我在示例中编辑了矩阵。谢谢!我想没有“简单”的问题我的一个问题是,我想把一个矩阵打印到一个需要一些特定格式的文件中,但我想我必须手动执行。
inline
void
my_print(const mat& X)
{
for(uword i=0; i < X.n_elem ++i) { cout << X(i) << ' '; }
}
inline
void
my_print(const mat& X)
{
for(uword row=0; row < X.n_rows; ++row)
{
for(uword col=0; col < X.n_cols; ++col) { cout << X(row,col) << ' '; }
// determine when to print newlines
if( row != (X.n_rows-1) ) { cout << '\n'; }
}
}
template<typename eT>
inline
void
my_print(const Mat<eT>& X)
{
for(uword row=0; row < X.n_rows; ++row)
{
for(uword col=0; col < X.n_cols; ++col) { cout << X(row,col) << ' '; }
// determine when to print newlines
if( row != (X.n_rows-1) ) { cout << '\n'; }
}
}
template<typename T1>
inline
void
my_print(const Base<typename T1::elem_type,T1>& expr)
{
const Mat<typename T1::elem_type> X(expr); // forcefully evaluate expression
for(uword row=0; row < X.n_rows; ++row)
{
for(uword col=0; col < X.n_cols; ++col) { cout << X(row,col) << ' '; }
// determine when to print newlines
if( row != (X.n_rows-1) ) { cout << '\n'; }
}
}