我该如何传输文件到C ++中提高的uBLAS矩阵? [英] How do I stream a file into a matrix in C++ boost ublas?
问题描述
我想在包含矩阵数据为提振矩阵文件阅读。 已经应该有这样的事情运算符重载,我可以把它写入标准流(COUT)。我不知道这有什么错走另一条路。我是相当新的C ++,所以我猜我正在做有关文件流的不正确的假设,但它好像它是有意义的。这里是网页我要去:
http://www.boost.org/ DOC /库/ 1_51_0 /升压/数字/ uBLAS库/ io.hpp
http://www.cplusplus.com/reference/iostream/ifstream/ifstream /
下面是我的code:
使用命名空间std;
矩阵LT;双>米;
ifstream的S(C:\\ TEMP \\ perm.txt,ifstream的::在);S>>米;
S.CLOSE();性病::法院LT&;<米;
下面就是我的文件看起来像:
[4,4]((0,0,1,0),(0,0,0,1),(0,1,0,0),(1 ,0,0,0))
下面是一个小例子,请尝试一下,看看会发生什么。如果这样做不行,我怀疑的问题是,文件路径是错误或程序未能从文本文件阅读:
的#include<升压/数字/ uBLAS库/ io.hpp>
#包括LT&;升压/数字/ uBLAS库/ matrix.hpp>
#包括LT&;&iostream的GT;
#包括LT&;&的fstream GT;诠释的main()
{
提高::数字:: uBLAS库::矩阵LT;双>米;
性病:: ifstream的S(C:\\ TEMP \\ perm.txt);
如果(!S)
{
性病::法院LT&;< 无法打开文件<<的std :: ENDL;
返回1;
}
如果(S>!> M)
{
性病::法院LT&;< 无法写入到矩阵<<的std :: ENDL;
返回1;
}
性病::法院LT&;< 打印矩阵;
性病::法院LT&;<米;
}
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/
Here's my code:
using namespace std;
matrix<double> M;
ifstream s("C:\temp\perm.txt", ifstream::in);
s >> M;
s.close();
std::cout << M;
Here's what my file looks like:
[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 ++中提高的uBLAS矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!