Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将文本文件读入矩阵,然后将它们相乘_C++_File Io_Matrix - Fatal编程技术网

C++ 将文本文件读入矩阵,然后将它们相乘

C++ 将文本文件读入矩阵,然后将它们相乘,c++,file-io,matrix,C++,File Io,Matrix,我有一些.txt文件,如下所示: 1050.00 68.13 1048.00 67.89 1046.00 67.62 1044.00 67.30 1042.00 66.91 [ ... ] 我想把它乘以另一个矩阵。 我的问题是我不知道如何在矩阵中读取和存储这些数据 有人能帮我吗?什么是“矩阵” 如果您有某种“矩阵库”,只需使用它的函数即可 如果您自己实现矩阵,请逐行读取文本文件(fgets())并使用sscanf()读取项目 希望这有帮助。什么是“矩阵” 如果您有某种“矩阵库”,只需

我有一些.txt文件,如下所示:

1050.00 68.13

1048.00 67.89

1046.00 67.62

1044.00 67.30

1042.00 66.91

[ ... ]
我想把它乘以另一个矩阵。

我的问题是我不知道如何在矩阵中读取和存储这些数据

有人能帮我吗?

什么是“矩阵”

如果您有某种“矩阵库”,只需使用它的函数即可

如果您自己实现矩阵,请逐行读取文本文件(fgets())并使用sscanf()读取项目

希望这有帮助。

什么是“矩阵”

如果您有某种“矩阵库”,只需使用它的函数即可

如果您自己实现矩阵,请逐行读取文本文件(fgets())并使用sscanf()读取项目


希望这有帮助。

您可以这样读:

char *fname = "matrix.txt";
ifstream infile(fname);

float f;
while (infile >> f) {
   //store f to matrix
}

web上有很多maxtrix类实现示例,其中有一个:

您可以这样阅读:

char *fname = "matrix.txt";
ifstream infile(fname);

float f;
while (infile >> f) {
   //store f to matrix
}

web上有很多maxtrix类实现示例,其中有一个:

我将数据存储在
向量中,并使用
std::getline
std::istringstream
操作符>
阅读它

#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <iterator>
#include <algorithm>
struct Matrix {
  std::vector<std::vector<double> > data;
  Matrix(const std::string& filename) {
    std::ifstream inFile(filename.c_str());
    std::string inLine;
    while(std::getline(inFile, inLine)) {
      std::istringstream inLineStream(inLine);
      std::vector<double> inLineData(
        (std::istream_iterator<double>(inLineStream)),
        std::istream_iterator<double>());
      data.push_back(inLineData);
    }
  }
  Matrix operator*(const Matrix& rhs) { ... };
};

int main () {
  Matrix a("a.txt");
  Matrix b("b.txt");
  Matrix c(a * b);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构矩阵{
std::矢量数据;
矩阵(常量std::字符串和文件名){
std::ifstream infle(filename.c_str());
std::字符串内联;
while(std::getline(infle,inLine)){
std::istringstream inLineStream(inLine);
向量内联数据(
(std::istream_迭代器(inLineStream)),
std::istreamu迭代器();
数据。推回(inLineData);
}
}
矩阵算子*(常数矩阵和rhs){…};
};
int main(){
矩阵a(“a.txt”);
矩阵b(“b.txt”);
矩阵c(a*b);
}

我将数据存储在
向量中,并使用
std::getline
std::istringstream
操作符>
读取数据

#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <iterator>
#include <algorithm>
struct Matrix {
  std::vector<std::vector<double> > data;
  Matrix(const std::string& filename) {
    std::ifstream inFile(filename.c_str());
    std::string inLine;
    while(std::getline(inFile, inLine)) {
      std::istringstream inLineStream(inLine);
      std::vector<double> inLineData(
        (std::istream_iterator<double>(inLineStream)),
        std::istream_iterator<double>());
      data.push_back(inLineData);
    }
  }
  Matrix operator*(const Matrix& rhs) { ... };
};

int main () {
  Matrix a("a.txt");
  Matrix b("b.txt");
  Matrix c(a * b);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构矩阵{
std::矢量数据;
矩阵(常量std::字符串和文件名){
std::ifstream infle(filename.c_str());
std::字符串内联;
while(std::getline(infle,inLine)){
std::istringstream inLineStream(inLine);
向量内联数据(
(std::istream_迭代器(inLineStream)),
std::istreamu迭代器();
数据。推回(inLineData);
}
}
矩阵算子*(常数矩阵和rhs){…};
};
int main(){
矩阵a(“a.txt”);
矩阵b(“b.txt”);
矩阵c(a*b);
}

欢迎使用堆栈溢出!如何读入数据部分取决于您希望如何在程序中表示数据。你为你的矩阵选择了一个数据结构吗?我想把它乘以另一个.txt文件,看起来也是这样。结果必须是积分。电话号码会来的。通过这个数字,我可以定义哪种颜色(带有RGB或类似的颜色)欢迎使用Stack Overflow!如何读入数据部分取决于您希望如何在程序中表示数据。你为你的矩阵选择了一个数据结构吗?我想把它乘以另一个.txt文件,看起来也是这样。结果必须是积分。电话号码会来的。用这个数字,我可以定义哪个颜色(带有RGB或类似的东西)注意这个问题是标记为C++。如果OP避免使用类型不安全的
fgets
sscanf
函数,可能是最好的选择。更喜欢<代码> STD::GETLION<代码>和<代码>操作符> <代码>。注意问题是C++。如果OP避免使用类型不安全的
fgets
sscanf
函数,可能是最好的选择。首选
std::getline
operator>