我的問題是獲取檔案中的所有整數,并存盤到一個 int 陣列(當然沒有|),然后用它做一些事情(這里我只需要幫助列印出陣列)。
檔案中的資料被稱為 10x10 矩陣。
當我嘗試使用std::stringstream.
輸入:
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
uj5u.com熱心網友回復:
兩個問題:當檔案中只有 10x10 時,您嘗試讀取 10x20。此外,您的代碼假定|所有相鄰數字之間都有一個,但事實并非如此。|一行的最后一個數字和下一行的第一個數字之間沒有。
相應地更改大小并在將它們拆分之前閱讀整行|:
string arr[10][10];
for(int i=0;i<10;i ){
std::string line;
getline(myfile,line);
std::stringstream linestream{line};
for(int j=0;j<10;j ){
getline(linestream,arr[i][j],'|');
}
}
現場演示
這是對您的代碼的最小更改。接下來,我建議直接從流中提取整數,而不是先提取字串,然后將它們轉換為整數(通過將字串放入另一個流中然后提取整數,您可以使用ifstream.
uj5u.com熱心網友回復:
基本問題是輸入檔案的內容不能很好地映射到您嘗試對該內容執行的操作。特別是,您假設輸入檔案中的每個字符都由一個|字符分隔,但情況并非如此,因為在任何特定行的最后一個字符和下一行的第一個字符之間都有換行符。
另外,請注意您創建了一個包含行和列的二維陣列,10但20您的輸入檔案只有10行和10列。
最好使用 2D std::vector,如下所示:
#include <iostream>
#include<sstream>
#include<string>
#include<vector>
#include <fstream>
int main()
{
std::ifstream inputFile("input.txt");
std::string line;
//create a 2D vector
std::vector<std::vector<int>> arr;
int num = 0; //this will hold the current integer value from the file
std::string numTemp;
if(inputFile)
{
while(std::getline(inputFile, line)) //read line by line
{
std::vector<int> temp;//create a 1D vector
std::istringstream ss(line);
while(std::getline(ss, numTemp, '|') && (std::istringstream(numTemp) >> num)) //read integer by integer
{
//emplace_back the current number num to the temp vector
temp.emplace_back(num);
}
//emplace_back the current 1D vector temp to the 2D vector arr
arr.emplace_back(temp);
}
}
else
{
std::cout<<"input file cannot be opened"<<std::endl;
}
//lets confirm if our 2D vector contains all the elements correctly
for(const auto& row: arr)
{
for(const auto& col: row)
{
std::cout<<col<<" ";
}
std::cout<<std::endl;
}
return 0;
}
上述程式的輸出可以在這里看到。
使用內置陣列的優點是:std::vector
- 您不必事先知道 input.txt 檔案中有多少行和多少列。
- 不必所有行都具有相同的列數。
即,使用std::vector靈活。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/435680.html
