如何在C ++中打印2D数组? [英] How to print 2D Arrays in C++?

查看:179
本文介绍了如何在C ++中打印2D数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用数组在屏幕上打印文本文件,但我不知道为什么它不会出现在文本文件中。



文本文件:

  1 2 3 4 
5 6 7 8

在应用废弃函数后,在屏幕上显示如下:

  1 
2
3
4
5
6
7
8

代码

  #include< iostream> 
#include< fstream>
#include< stdlib.h>
#include< string>

using namespace std;

const int MAX_SIZE = 20;
const int TOTAL_AID = 4;

void discard_line(ifstream& in);
void print(int print [] [4],int size);

int main()
{
// string evnt_id [MAX_SIZE]; // stores event id
int athlete_id [MAX_SIZE] [TOTAL_AID]; //存储athelete id的列
int total_records;
char c;
ifstream reg;
reg.open(C:\\result.txt);

discard_line(reg);
total_records = 0;

while(!reg.eof())
{
for(int i = 0; i {
reg >> athlete_id [total_records] [i]; // read aid coloumns
}
total_records ++;
reg.get(c);
}

reg.close();

print(athlete_id,total_records);

系统(pause);
return 0;
}

void discard_line(ifstream& in)
{
char c;

do
in.get(c);
while(c!='\\\
');
}

void print(int print [] [4],int size)
{
cout< \tID \t AID< endl;
for(int i = 0; i {
for(int j = 0; j {
cout < print [i] [j]< endl;
}
}
}


解决方案>

您正在每个数字后面打印 std :: endl 。如果你想每行有一行,那么你应该在每行后面打印 std :: endl 。示例:

  #include< iostream& 

int main(void)
{
int myArray [] [4] = {{1,2,3,4},{5,6,7,8} };
int width = 4,height = 2;

for(int i = 0; i {
for(int j = 0; j< width; ++ j)
{
std :: cout<< myArray [i] [j]< '';
}
std :: cout<< std :: endl;
}
}

命名空间std; 在文件开头被认为是坏习惯,因为它导致一些用户定义的名称(类型,函数等)变得模糊。如果你想避免使用 std :: 穷尽前缀,在小范围内使用使用namespace std; 和其他文件不受影响。


I am trying to print a text file out on screen using arrays, but I'm not sure why it does not appear the way it is in the text file.

The text file:

1 2 3 4
5 6 7 8

Displayed on the screen as follows after applying discard function:

1
2
3
4
5
6
7
8

The code:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;

const int MAX_SIZE = 20;
const int TOTAL_AID = 4;

void discard_line(ifstream &in);
void print(int print[][4] , int size);

int main()
{
    //string evnt_id[MAX_SIZE]; //stores event id
    int athlete_id[MAX_SIZE][TOTAL_AID]; //stores columns for athelete id
    int total_records;
    char c; 
    ifstream reg;
    reg.open("C:\\result.txt");

    discard_line(reg);
    total_records = 0;

    while( !reg.eof() )
    {
        for (int i = 0; i < TOTAL_AID; i++)
        {
            reg >> athlete_id[total_records][i] ;//read aid coloumns
        }
        total_records++;
        reg.get(c);
    }

    reg.close();

    print(athlete_id, total_records);

    system("pause");
    return 0;
}

void discard_line(ifstream &in)
{
    char c;

    do
        in.get(c);
    while (c!='\n');
}

void print(int print[][4] , int size)
{    
    cout << " \tID \t AID " << endl;
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < TOTAL_AID; j++)
        {
            cout << print[i][j] << endl;
        }           
    }
}    

解决方案

You are printing std::endl after each number. If you want to have 1 row per line, then you should print std::endl after each row. Example:

#include <iostream>

int main(void)
{
    int myArray[][4] = { {1,2,3,4}, {5,6,7,8} };
    int width = 4, height = 2;

    for (int i = 0; i < height; ++i)
    {
        for (int j = 0; j < width; ++j)
        {
            std::cout << myArray[i][j] << ' ';
        }
        std::cout << std::endl;
    }
}

Also note that writing using namespace std; at the beginning of your files is considered bad practice since it causes some of user-defined names (of types, functions, etc.) to become ambiguous. If you want to avoid exhausting prefixing with std::, use using namespace std; within small scopes so that other functions and other files are not affected.

这篇关于如何在C ++中打印2D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆