我如何将一个文件流到一个矩阵中的C ++ boost ublas? [英] How do I stream a file into a matrix in C++ boost ublas?
问题描述
我想读取一个包含矩阵数据的文件到boost矩阵中。 已经被假定为这种事情的运算符重载,我可以让它写一个标准流(cout)。我不知道对方走什么错了。我对C ++相当新,所以我猜我对文件流做了一个不正确的假设,但它似乎是有意义的。以下是我要进行的网页:
I'm trying to read in a file that contains matrix data into a boost matrix. "" is already supposed to have operator overloads for this sort of thing and I can get it to write to a standard stream (cout). I don't know what's wrong with going the other way. I'm fairly new to C++, so I'm guessing I'm making an incorrect assumption regarding file streams, but it seemed like it made sense. Here are the web pages I'm going on:
http://www.boost.org/doc/libs/1_51_0/boost/numeric/ublas/io.hpp
http://www.cplusplus.com/reference/iostream / ifstream / ifstream /
这是我的代码:
using namespace std;
matrix<double> M;
ifstream s("C:\temp\perm.txt", ifstream::in);
s >> M;
s.close();
std::cout << M;
这是我的档案:
[4,4]((0,0,1,0),(0,0,0,1),(0,1,0,0),(1,0,0,0))
推荐答案
,请尝试一下,看看会发生什么。如果这不工作,我怀疑问题是文件路径错误或程序未能从文本文件读取:
Here is a small example, please try it out and see what happens. If this doesn't work, I suspect that the problem is that the file path is wrong or the program is failing to read from the text file:
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <iostream>
#include <fstream>
int main()
{
boost::numeric::ublas::matrix<double> m;
std::ifstream s("C:\temp\perm.txt");
if (!s)
{
std::cout << "Failed to open file" << std::endl;
return 1;
}
if (!s >> m)
{
std::cout << "Failed to write to matrix" << std::endl;
return 1;
}
std::cout << "Printing matrix: ";
std::cout << m;
}
这篇关于我如何将一个文件流到一个矩阵中的C ++ boost ublas?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!