我有一个名为matrices.txt的文件,它有两个3乘3的矩阵。 他们是:
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
我正在尝试从这个文件读取并将其存储到一个数组proto_matrix中,以便稍后将其分成两个矩阵。
我遇到的问题是我无法将数字存储在数组中。
我的代码是
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
int i = 0, j, k = 0, n, dimension;
cout << "Enter the dimension of square matrices (3 by 3 would be 3) \n";
cin >> n;
dimension = n * n;
int proto_matrix[2 * dimension];
// make array of two matrices combined, this will be split into two matrices
ifstream matrix_file("matrices.txt");
while(matrix_file)
{
matrix_file >> proto_matrix[i];
}
matrix_file.close();
}
我尝试调试代码,似乎没有整数存储在数组中,只是随机数。
答案 0 :(得分:0)
此:
int proto_matrix[2 * dimension];
是可变长度数组(VLA),它不是标准C ++,但受某些编译器扩展支持。如果使用g++ -pedantic main.cpp
进行编译,肯定会出错。
这意味着如果 使用数组,则需要动态分配内存。
你真的不喜欢,C ++提供std::vector
,它可以相对容易地实现你想要的。我比较说,因为你在一个文件中有两个矩阵,这使得解析文件有点棘手。通常我们将一个矩阵放在一个文件中,另一个矩阵放在另一个文件中。
因此,我将使用大小为2(固定大小!)的数组,其中每个元素都将成为2D矢量。
此外,我将跟踪我正在从文件中读取的矩阵(第一个或第二个),以及我当前读取了多少行,以便填充矩阵的正确单元格。
index
将等于0或1,以索引数组。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
int i = 0, j, k = 0, n, dimension;
cout << "Enter the dimension of square matrices (3 by 3 would be 3) \n";
cin >> n;
dimension = n * n;
vector< vector<int> > v[2];
v[0].resize(n);
v[1].resize(n);
// make array of two matrices combined, this will be split into two matrices
ifstream matrix_file("matrices.txt");
string line;
int rows_read = 0, cols_read = 0, index = 0;
while (std::getline(matrix_file, line))
{
std::istringstream iss(line);
int a, b, c;
if (!(iss >> a >> b >> c)) { break; } // error
// process tuple (a, b, c)
if(index == 0 && rows_read >= n)
{
rows_read = 0;
index = 1;
}
//cout<<"rows = " << rows_read << " " << index<<endl;
v[index][rows_read].push_back(a);
v[index][rows_read].push_back(b);
v[index][rows_read++].push_back(c);
}
matrix_file.close();
for(int i = 0; i < 2; ++i)
{
cout << "Printing matrix " << i << endl;
for(auto& matrix: v[i])
{
for(auto& number: matrix)
cout << number << " ";
cout << endl;
}
}
return 0;
}
输出:
Printing matrix 0
1 2 3
4 5 6
7 8 9
Printing matrix 1
1 2 3
4 5 6
7 8 9
PS:与您的问题无关,但是What should main() return in C and C++?一个int
。
答案 1 :(得分:0)
基于我的answer,我将在这里发布一个更清晰的示例,它实现了代码尝试实现的目标:读取一维数据结构中的所有数字(稍后将以某种方式拆分为两个矩阵)。
因此,对于这种情况,代码归结为:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
int i = 0, j, k = 0, n, dimension;
cout << "Enter the dimension of square matrices (3 by 3 would be 3) \n";
cin >> n;
dimension = n * n;
// make array of two matrices combined, this will be split into two matrices
vector<int> proto_matrix;
ifstream matrix_file("matrices.txt");
string line;
while (std::getline(matrix_file, line))
{
std::istringstream iss(line);
int a, b, c;
if (!(iss >> a >> b >> c)) { break; } // error
proto_matrix.push_back(a);
proto_matrix.push_back(b);
proto_matrix.push_back(c);
}
matrix_file.close();
for(auto& number: proto_matrix)
cout << number << "\n";
return 0;
}
答案 2 :(得分:0)
您不会增加i
,因此始终会写入第一个元素。它应该是:
matrix_file >> proto_matrix[i++];
答案 3 :(得分:0)
您可以将所有值读入 flat 容器(例如std::vector
)并稍后重新安排。以下代码可能会给您一个想法:
#include <vector>
#include <fstream>
#include <iostream>
int main() {
std::vector<int> values;
std::ifstream fp("matrices.txt");
for (int value; fp >> value; ) {
values.emplace_back(value);
}
std::cout << "I read " << values.size() << " values:\n";
for (auto && value : values) std::cout << value << " ";
std::cout << "\n";
}
结果:
$ clang++ matrixread.cpp -std=c++17
$ ./a.out
I read 18 values:
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9