C ++ 2d数组显示功能

时间:2016-11-16 11:35:03

标签: c++ arrays cout

我想知道是否有人可以帮助我,我在下面制作了这个简单的程序来尝试使用2d数组。我目前正在使用cout来显示信息,但我想使用显示功能,但我不确定如何正确地执行此操作。

任何帮助将不胜感激。

(评论部分只是在我运行程序时显示输出cout)

 #include <iostream>
    #include <array>
    using namespace std;

    int main()
    {   

        int [3][3] = { {},{},{} };

        cout <<"    " << "  " << "" << "    " << "" << endl;
        cout << "   "<< m[0][0] << "    " << m[0][1] << "   " << m[0][2] << endl;
        cout << "   " << m[1][0] << "   " << m[1][1] << "   " << m[1][2] << endl;
        cout << "   " << m[2][0] << "   " << m[2][1] << "   " << m[2][2] << endl;

    return 0;
    }

3 个答案:

答案 0 :(得分:0)

这是您的2D阵列的显示功能:

void print_it(const int m[3][3]){
    cout << "Medal    " << 1 << '\t' << 2 << '\t' << 3 << endl;
    cout << '\n';
    cout << "Country1 " << m[0][0] << '\t' << m[0][1] << '\t' << m[0][2] << << '\n';
    cout << "Country2 " << m[1][0] << '\t' << m[1][1] << '\t' << m[1][2] << << '\n';
    cout << "Country3 " << m[2][0] << '\t' << m[2][1] << '\t' << m[2][2] << << '\n';

}

简单地在你的main()中调用它:

int main()
{

    int m[3][3] = { { 2, 1, 8 }, { 5, 3, 9 }, { 2, 0, 9 } };

    print_it(m);

    return 0;
}

嗯,这不是一个好的(不是现代的)实现。这当然只适用于大小为3 x 3的数组(除非你去指针,传递大小......但这会变得混乱)。因此,请考虑使用现代容器,例如<vector>(或<array>,如您所包含的那样)。比你可以打印任何n x m阵列:

#include <vector>
#include <iostream>

void print_it(const std::vector<std::vector<int>> &m){
    std::cout << "Medal    ";
    for (int i = 0; i < m[0].size(); ++i){
        std::cout << i << '\t';
    }

    std::cout << "\n---------------------------------\n";

    for (int y = 0; y < m.size(); ++y){
        std::cout << "Country" << y + 1 << ' ';
        for (int x = 0; x < m[0].size(); ++x){
            std::cout << m[y][x] << '\t';
        }
        std::cout << '\n';
    }

调用它

int main(){
    std::vector<std::vector<int>> m{ { 2, 1, 8 }, { 5, 3, 9 }, { 2, 0, 9 } };

    print_it(m);

    return 0;
}

您可以获得所需的输出:

Medal    0      1       2
---------------------------------
Country1 2      1       8
Country2 5      3       9
Country3 2      0       9

注意,您可以将m更改为任何n x m数组,例如:

std::vector<std::vector<int>> m{ { 2, 5 }, { 9, 5 }, { 10, 15 }, { 0, 2 }, { 5, 7 } };
print_it(m);

得到:

Medal    0      1
---------------------------------
Country1 2      5
Country2 9      5
Country3 10     15
Country4 0      2
Country5 5      7

答案 1 :(得分:0)

尝试类似以下内容

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

template <class T, size_t M, size_t N>
std::ostream & display( const T ( &a )[M][N], 
                        const std::string &header, 
                        const std::string &name, 
                        std::ostream &os = std::cout )
{
    os << std::setw( name.size() + header.size() + 3 );

    for ( size_t i = 0; i < N; i++ )
    {
        os << header << std::setw( 3 ) << std::left << i + 1;
        os << ( ( i + 1 ) % N == 0 ? '\n' : ' ' );
    }

    for ( size_t i = 0; i < M; i++ )
    {
        os << name << std::left << i + 1;
        for ( size_t j = 0; j < N; j++ )
        {
            os << std::setw( header.size() + 3 ) << std::right << a[i][j];
            os << ( ( j + 1 ) % N == 0 ? '\n' : ' ' );

        }
    }

    os << std::endl;

    return os;
}   

int main()
{   
    int m[3][3] = 
    { 
        { 2, 1, 8 },
        { 5, 3, 9 },
        { 2, 0, 9 } 
    };


    display( m, "Medal", "Country" );

    return 0;
}

程序输出

          Medal1   Medal2   Medal3  
Country1       2        1        8
Country2       5        3        9
Country3       2        0        9

答案 2 :(得分:0)

    int m[3][3] = { {2,1,8},{5,3,9},{2,0,9} };
    display(m);

显示程序:

void display(int m[3][3])
{
       cout <<"    Medal1" << "    " << "Medal2" << "  " << "Medal3" << endl;
    for (int i=0 ;i<3;i++){
        cout << "Country"<<i+1<<"     ";
        for (int j=0 ;j<3;j++){
    cout<< m[i][j] << "    " ;
    if(j==2)
        cout<<endl;
        }}

}

希望这有帮助。