[英]Reading arbitrary array of any size
當讀取包含兩個5X5數組的兩個.txt文件時,以下代碼可以正常工作。
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
string myFile, mysecondFile, mystring;
string DIR;
string extension;
int total = 0;
int number_of_lines = 0;
string line;
extension = ".txt";
DIR = "H:\\Year2\\EE273\\EE273\\Week6\\";
cout << "Enter the name of the file: \t";
cin >> myFile;
cout << "Enter the name of the second file: \t";
cin >> mysecondFile;
myFile = DIR + myFile + extension;
mysecondFile = DIR + mysecondFile + extension;
ifstream inFile;
ifstream inFile2;
int i=5;
int j=5;
int i2=5;
int j2=5;
int i3=5;
int j3=5;
int k;
int l;
int Array[5][5];
int Array2[5][5];
int Array3[5][5];
string attempt1,attempt2;
int row = 0;
int col = 0;
int row2 = 0;
int col2 = 0;//i = row
//y = column
inFile.open(myFile.c_str());
if (!inFile) {
cout <<"Error opening file"<<myFile<<endl;
return -1;
}
while (!inFile.eof())
{
getline(inFile, attempt1);
stringstream iss( attempt1 );
string result;
col = 0;
while (getline( iss, result, ','))
{
//cout << result << endl;
Array[row][col] = atoi(result.c_str());
//j = j + 1;
col = col + 1;
}
row = row + 1;
}
inFile.close();
inFile2.open(mysecondFile.c_str());
if (!inFile2) {
cout <<"Error opening file"<<mysecondFile<<endl;
return -1;
}
while (!inFile2.eof())
{
getline(inFile2, attempt2);
stringstream iss( attempt2 );
string result2;
col2 = 0;
while (getline( iss, result2, ','))
{
//cout << result2 << endl;
Array2[row2][col2] = atoi(result2.c_str());
col2 = col2 + 1;
}
row2 = row2 + 1;
}
inFile2.close();
/*for (int i=0;i<5;i++){
for (int j=0; j<5; j++){
cout<<Array[i][j]<<endl;}}
for (int i2=0;i2<5;i2++){
for (int j2=0; j2<5; j2++){
cout<<Array2[i2][j2]<<endl;
}}
在這里,我執行兩個矩陣之間的乘法,並將結果值寫入第三個矩陣。
int Total=0;
i=0;
j2=0;
j=0;
j3=0;
for (i3=0; i3<5; i3++) {
while(j3<5){
while (j<5){
for (i2=0;i2<5;i2++){
Total += Array[i][j]*Array2[i2][j2];
j++;
Array3[i3][j3]=Total;
}}
j=0;
j2++;
j3++;
Total=0;
}
i++;
j=0;
j2=0;
j3=0;
Total=0;
}
我的問題是:修改代碼的最簡單方法是什么,以便它可以讀取包含任意大小數組的兩個.txt文件,然后成功執行乘法?
編輯我必須使用數組,不幸的是我不能使用矢量。
我認為新的運營商參與了嗎?
“最簡單”的方法是做一些天真的事情,比如完全讀取文件一次以獲得行數/列數,然后再次讀取文件以實際存儲矩陣中的值:
unsigned int rows = 0;
unsigned int cols = 0;
std::string line;
while (std::getline(inFile, line)) {
rows++;
std::stringstream ss(line);
std::string col;
while (std::getline(ss, col, ',')) {
cols++;
}
}
// Now allocate the rows*cols matrix
int** matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols];
}
// and read your values into the matrix ...
// matrix[m][n] = xxx
讀取文件兩次效率非常低; 還有其他方法可以預先獲得尺寸。 例如,您可以在輸入文件中使用約定來在數據之前包含矩陣寬度/高度:
[infile.txt]
3,3
1,2,3
4,5,6
7,8,9
現在您可以讀取文件的第一行,並且您將知道該文件的其余部分包含3x3矩陣。 使用new
分配矩陣(類似於上面的示例),然后繼續讀取文件的其余部分。
請記住使用delete[]
清理動態分配的矩陣。 每次調用new
都應該有1次delete
調用。
for (int i = 0; i < rows; i++) {
delete[] matrix[i];
}
delete[] matrix;
使用std::vector
而不是原始數組。 例如,你可以在向量上push_back
一個項目。 更重要的是,您可以使用僅在運行時知道的大小創建它,例如從文件中的信息創建。
最簡單的方法要求文件包含矩陣的大小作為其第一個條目。 有了這個,您可以回退到使用C(C ++不能容忍動態大小的矩陣)並執行以下操作:
將矩陣的維度讀入變量width
和heigh
。
使用分配矩陣
int (*dynamicMatrix)[width] = malloc(height*sizeof(*dynamicMatrix));
重用代碼填充矩陣。
如果你不能回退到C,並且不能使用std::vector<>
,那么唯一剩下的就是使用雙指針:
int**dynamicMatrix = new int*[height];
for(size_t i = width; i--; ) dynamicMatrix[i] = new int[width];
同樣,如果您可以在文件中定義前兩個數字以包含文件中矩陣的寬度和高度,則這是最簡單的。 如果您無法將這兩個數字編碼到文件中,則必須隨時增加動態數組:
size_t lines = 0, allocatedLines = 8;
int** dynamicMatrix = new int*[allocatedLines];
while(/* can read a line */) {
if(lines == allocatedLines) {
int** temp = new int*[allocatedLines *= 2];
for(size_t i = lines; i--; ) temp[i] = dynamicMatrix[i];
delete[] dynamicMatrix;
dynamicMatrix = temp;
}
//add one line
size_t curLineLength = 0, allocatedLineLength = 8;
dynamicMatrix[lines++] = new int[allocatedLineLength];
//fill the line
...
}
用於重新分配線的類似塊將需要進入循環,在該循環中讀取單行的元素。 這很乏味; 但要變得更好的唯一方法是使用不允許使用的東西。
順便說一句:即使重新分配的東西在C中也更容易,因為它提供了realloc()
函數:
size_t lines = 0, allocatedLines = 8;
int** dynamicMatrix = malloc(allocatedLines * sizeof(*dynamicMatrix));
while(/* can read a line */) {
if(lines == allocatedLines) {
//realloc takes care of copying the data to a new location (if that is necessary):
allocatedLines *= 2;
dynamicMatrix = realloc(dynamicMatrix, allocatedLines * sizeof(*dynamicMatrix));
}
//add one line
size_t curLineLength = 0, allocatedLineLength = 8;
dynamicMatrix[lines++] = malloc(allocatedLineLength * sizeof(**dynamicMatrix));
//fill the line
...
}
由於沒有等效的realloc()
來處理new
/ delete
,因此您需要在C ++中使用std::vector<>
,或者像上面那樣自己進行復制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.