【问题标题】:c++ print formatted 2D arrayc++打印格式化二维数组
【发布时间】:2021-10-09 09:40:02
【问题描述】:

我正在尝试用 C++ 打印一个二维矩阵。我有一个二维整数数组。输出如下所示:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 
0 0 0 0 0 0 0 0 0 0 60 60 60 60 60 60 60 60 60 60 100 100 100 100 100 100 100 100 100 100 160 

我的代码只是执行 2 次循环,并在每个数字后添加一个空格(并在每一行后添加一个换行符)。有没有一种简单的方法可以在 cpp 中打印格式良好的矩阵。像这样更具可读性的东西:

0 0 0 0 0 0 0  0  0   0   0 
0 0 0 0 0 0 60 60 60  60  60
0 0 0 0 0 0 60 60 100 100 160

代码:

for(int i = 0; i <= n ; i++){
    for(int w = 0; w <= W ; w++){
        std:cout<<some_array[i][w]<<"   ";
    }
    std::cout << std::endl;
}

【问题讨论】:

  • 这能回答你的问题吗? How to print 2D Arrays in C++?
  • Is there an easy way 不。但是这样做,编写代码,遍历每个元素,获取元素打印的宽度,记住每列中最宽的元素,再次重复每个元素,用空格填充元素提取列宽。
  • 太好了,我会附上代码来做一下,寻找一种简单的方法,因为它看起来像很多人可能经常需要的东西。
  • for(int i = 0; i &lt;= n ; i++) -- 任何以&lt;= 为循环条件的循环都是可疑的,因为它具有所有迹象都表明缓冲区溢出。
  • 只需使用 std:cout&lt;&lt; setfill(5) &lt;&lt; some_array[i][w]; 之类的东西来打印大小为 5 的字段中的值。

标签: c++ arrays


【解决方案1】:

输出流运算符的这种重载将为您进行格式化。 然后你输出的代码会看起来很干净。

#include <iostream>

template<typename type_t, std::size_t rows_v, std::size_t cols_v>
std::ostream& operator<<(std::ostream& os, type_t (&arr)[rows_v][cols_v])
{
    // loop over the rows
    for (const auto& row : arr)
    {
        // to print a comma between values
        bool comma{ false };

        // loop over the values in the row
        for (const auto& value : row)
        {
            if (comma) os << ", ";
            os << value;
            comma = true;
        }
        os << "\n";
    }

    return os;
}

int main()
{
    int arr[2][3]{{0,1,2},{4,5,6}};
    std::cout << arr << "\n";
    return 0;
}

【讨论】:

    【解决方案2】:

    执行此操作的快速代码可以做得更好:

    #include <iostream>
    #include <string>
    
    int main()
    {
        int maxwidth = 0;
        int sz;
        std::string s;
    
        int K[3][31] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60},
                    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 160}};
    
        for (int i = 0; i < 3; i++)
        {
            for (int w = 0; w < 31; w++)
            {
                s = std::to_string(K[i][w]);
                sz = s.size();
                maxwidth = std::max(maxwidth, sz);
            }
        }
    
        maxwidth++; // we need to print 1 extra space than maxwidth
    
        for (int i = 0; i < 3; i++)
        {
            for (int w = 0; w < 31; w++)
            {
                std::cout << K[i][w];
                s = std::to_string(K[i][w]);
                sz = (maxwidth - s.size());
                while (sz--)
                    std::cout << " ";
            }
            std::cout << std::endl;
        }
        return 0;
    }
    

    输出:

    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   
    0   0   0   0   0   0   0   0   0   0   60  60  60  60  60  60  60  60  60  60  60  60  60  60  60  60  60  60  60  60  60  
    0   0   0   0   0   0   0   0   0   0   60  60  60  60  60  60  60  60  60  60  100 100 100 100 100 100 100 100 100 100 160 
    

    【讨论】:

      猜你喜欢
      • 2018-12-02
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      • 2011-07-01
      • 2023-03-04
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多