将包含特殊字符的字符串数组转换为 int 数组。输入来自文件
我这里的问题是获取文件中的所有整数,并将其存储到 int 数组中(当然没有 |
),然后用它做一些事情(这里我只需要帮助来打印数组)。
文件中的数据据说是一个 10x10 矩阵。
当我尝试使用 std::stringstream 时,我的代码输出是另一个带有垃圾值的 10x10。
输入:
1|11|2|13|2|2|2|11|13|2
1|11|2|13|2|2|2|13|2|11
1|13|1|13|2|2|2|2|2|2
1|13|1|12|2|2|2|2|2|2
1|13|1|13|1|2|2|2|2|2
1|13|1|13|1|1|2|2|2|2
1|13|1|13|1|1|1|2|2|2
1|13|1|13|1|1|1|1|2|2
1|13|1|13|1|1|1|1|1|2
1|13|1|13|1|1|1|1|1|1
代码:
#include<iostream>
#include<cstring>
#include<fstream>
#include<sstream>
using namespace std;
int main(){
//read data from file and store in a 2d array
ifstream myfile("input.txt");
string arr[10][20];
for(int i=0;i<10;i++){
for(int j=0;j<20;j++){
getline(myfile,arr[i][j],'|');
}
}
//convert string to int
//use stringstream
int arr2[10][20];
for(int i=0;i<10;i++){
for(int j=0;j<20;j++){
stringstream ss;
ss<<arr[i][j];
ss>>arr2[i][j];
}
}
//print arr2
for(int i=0;i<10;i++){
for(int j=0;j<20;j++){
cout<<arr2[i][j]<<" ";
}
cout<<endl;
}
myfile.close();
return 0;
}
输出应该是:
1 11 2 13 2 2 2 11 13 2
1 11 2 13 2 2 2 13 2 11
1 13 1 13 2 2 2 2 2 2
1 13 1 12 2 2 2 2 2 2
1 13 1 13 1 2 2 2 2 2
1 13 1 13 1 1 2 2 2 2
1 13 1 13 1 1 1 2 2 2
1 13 1 13 1 1 1 1 2 2
1 13 1 13 1 1 1 1 1 2
1 13 1 13 1 1 1 1 1 1
我的输出:
1 11 2 13 2 2 2 11 13 2
1 13 2 2 2 2 2 2 13 1
1 2 2 2 2 2 13 1 13 1
1 2 2 2 13 1 13 1 1 1
1 2 13 1 13 1 1 1 1 1
1994842136 12 7086696 7085352 1994774642 0 1994842144 1994771436 0 0
6416848 2004213955 7086772 6416972 12 7086696 0 4 48 1994842112
2004213726 1 7149280 7149288 7086696 0 1988425164 0 2003400672 12
1999860416 6416972 2 1999966512 1999657456 1999691632 1999846272 1999845632 1999819744 1999860464
4194304 1994719472 0 6417024 0 6418312 2004471984 775517426 -2 6417120
My problems here is to take all the integers in a file, and store to an int array (of course without |
), and then do something with it (here I just need help to print out the array).
The data from the file is said to be a 10x10 matrix.
My code output is another 10x10 with trash values, as I tried using std::stringstream
.
Input:
1|11|2|13|2|2|2|11|13|2
1|11|2|13|2|2|2|13|2|11
1|13|1|13|2|2|2|2|2|2
1|13|1|12|2|2|2|2|2|2
1|13|1|13|1|2|2|2|2|2
1|13|1|13|1|1|2|2|2|2
1|13|1|13|1|1|1|2|2|2
1|13|1|13|1|1|1|1|2|2
1|13|1|13|1|1|1|1|1|2
1|13|1|13|1|1|1|1|1|1
Code:
#include<iostream>
#include<cstring>
#include<fstream>
#include<sstream>
using namespace std;
int main(){
//read data from file and store in a 2d array
ifstream myfile("input.txt");
string arr[10][20];
for(int i=0;i<10;i++){
for(int j=0;j<20;j++){
getline(myfile,arr[i][j],'|');
}
}
//convert string to int
//use stringstream
int arr2[10][20];
for(int i=0;i<10;i++){
for(int j=0;j<20;j++){
stringstream ss;
ss<<arr[i][j];
ss>>arr2[i][j];
}
}
//print arr2
for(int i=0;i<10;i++){
for(int j=0;j<20;j++){
cout<<arr2[i][j]<<" ";
}
cout<<endl;
}
myfile.close();
return 0;
}
The output should be:
1 11 2 13 2 2 2 11 13 2
1 11 2 13 2 2 2 13 2 11
1 13 1 13 2 2 2 2 2 2
1 13 1 12 2 2 2 2 2 2
1 13 1 13 1 2 2 2 2 2
1 13 1 13 1 1 2 2 2 2
1 13 1 13 1 1 1 2 2 2
1 13 1 13 1 1 1 1 2 2
1 13 1 13 1 1 1 1 1 2
1 13 1 13 1 1 1 1 1 1
My output:
1 11 2 13 2 2 2 11 13 2
1 13 2 2 2 2 2 2 13 1
1 2 2 2 2 2 13 1 13 1
1 2 2 2 13 1 13 1 1 1
1 2 13 1 13 1 1 1 1 1
1994842136 12 7086696 7085352 1994774642 0 1994842144 1994771436 0 0
6416848 2004213955 7086772 6416972 12 7086696 0 4 48 1994842112
2004213726 1 7149280 7149288 7086696 0 1988425164 0 2003400672 12
1999860416 6416972 2 1999966512 1999657456 1999691632 1999846272 1999845632 1999819744 1999860464
4194304 1994719472 0 6417024 0 6418312 2004471984 775517426 -2 6417120
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个问题:当文件中只有 10x10 时,您尝试读取 10x20。此外,您的代码假设所有相邻数字之间都有
|
,但事实并非如此。一行中的最后一个数字与下一行中的第一个数字之间没有|
。相应地更改大小并在
|
处分割它们之前阅读整行:现场演示
这是对代码的最小更改。接下来,我建议直接从流中提取整数,而不是首先提取字符串,然后将它们转换为整数(通过将字符串放入另一个流中,然后提取整数,您可以使用 ifstream 来完成此操作>)。
Two issues: You try to read 10x20 when there is only 10x10 in the file. Further your code assumes that there is a
|
between all adjacent numbers, but thats not the case. There is no|
between the last number in a line and the first in the next line.Change the size accordingly and read full lines before you split them at
|
:Live Demo
This is minimum changes on your code. Next I would suggest to extract integers from the stream directly instead of first extracting string and then convert them to integers (by placing the strings into another stream and then extracting the integer, you could do this already with the
ifstream
).基本的问题是输入文件的内容无法很好地映射到您尝试对该内容执行的操作。特别是,您假设输入文件中的每个字符都由
|
字符分隔,但事实并非如此,因为任何特定行中的最后一个字符与中的第一个字符之间都有换行符下一行。另请注意,您创建了一个具有
10
行和20
列的二维数组,但您的输入文件只有10
行和>10
列。更好的方法是使用 2D
std::vector
,如下所示:上述程序的输出可以看到 此处。
与内置数组相比,使用 std::vector 的优点是:
也就是说,使用 std::vector 是灵活的。
The basic problem is that the content of your input file doesn't map well to the operation that you're trying to do on that content. In particular, you've assumed that every character in your input file is separated by a
|
character which is not the case since there is a newline between the last character in any particular line and the first character in the next line.Also, do note that you have created a 2D array that has
10
rows and20
columns but your input file has only10
rows and10
columns.Better would be to use a 2D
std::vector
as shown below:The output of the above program can be seen here.
The advantage of using
std::vector
over built in array is that :That is, using
std::vector
is flexible.