c++ - 以线的形式显示 Boost 矩阵。想改成盒子

标签 c++ boost

//这个简单的程序初始化了一个3x3的矩阵,并以线的形式输出。我想以矩阵的形式显示这个矩阵。

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

int main(){
  using namespace boost::numeric::ublas;
  matrix<double> m1(3,3);

 for(unsigned i=0;i<m1.size1();++i)
 for (unsigned j=0;j<m1.size2();++j)
     m(i,j)=3*i*j;
 std::cout<<m1<<std::endl;
return 0;
}
output
[3,3]((0,0,0),(0,3,6),(0,6,12)

最佳答案

你可以这样做

void printMatrix(const matrix<double> &m)
{
    for(unsigned i=0;i<m.size1();++i)
    {
        cout<<"| ";
        for (unsigned j=0;j<m.size2();++j)
        {
            cout<<m(i,j)<<" | ";
        }
        cout<<"|"<<endl;
    }
}

这将打印如下内容:

| 0 | 0 | 0 |
| 0 | 3 | 6 |
| 0 | 6 | 9 |

关于c++ - 以线的形式显示 Boost 矩阵。想改成盒子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30896012/

相关文章:

c++ - 如何将元素插入 multimap ?

c++ - 将秒转换为时分秒

c++ - 用opengl绘制子像素

c++ - C/C++ #include 格式化最佳实践

C++ - 智能指针 - 通过模板将派生类共享指针传递给基类

c++ - Boost.Python : how to create & return a list of existing c++ objects, 没有复制

c++ - G++ 错误 :/usr/lib/rpm/redhat/redhat-hardened-cc1: No such file or directory

c++本地时间不受使用boost的夏令时的影响

c++ - 用 min_element boost 信号

c++ - 将 boost 日期初始化为公元元年