如何在 C++ 中打印二维数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12311149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print 2D Arrays in C++?
提问by Nick
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;
}
}
}
回答by LihO
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:
您std::endl
在每个数字后打印。如果你想每行有 1 行,那么你应该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 < 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.
另请注意,using namespace std;
在文件开头写入被认为是不好的做法,因为它会导致某些用户定义的名称(类型、函数等)变得不明确。如果您想避免用 前缀用尽std::
,请using namespace std;
在小范围内使用,以免影响其他功能和其他文件。
回答by MichaelGD
It is not only mistake that you miss the "endl". The program will also skip the first line in the source file because of calling the function discard_line(reg), so you only can get the others data(5 6 7 8). It is not necessary to use the function at all. in addition, make sure that you init the array and check boundary of array, such as MAX_SIZE, to guarantee the input data not to overflow the array.
错过“endl”不仅是错误。由于调用了discard_line(reg)函数,程序也会跳过源文件的第一行,所以只能得到其他数据(5 6 7 8)。完全没有必要使用该功能。另外,请确保您初始化数组并检查数组的边界,例如MAX_SIZE,以保证输入数据不会溢出数组。