【发布时间】:2015-06-01 05:43:31
【问题描述】:
我有一个 Boost ublas 矩阵,我想将它的内容打印到一个文本文件中。我有以下实现,它可以工作。
#include <iostream>
using namespace std;
#include "boost\numeric\ublas\matrix.hpp"
typedef boost::numeric::ublas::matrix<float> matrix;
#include <algorithm>
#include <iterator>
#include <fstream>
int main()
{
fill(m1.begin2(), m1.begin2() + 400 * 500, 3.3);
ofstream dat("file.txt");
for (auto i = 0; i < 400 ; i++) {
for (auto j = 0; j < 500; j++) {
dat << m1(i, j) << "\t"; // Must seperate with Tab
}
dat << endl; // Must write on New line
}
我想在不使用嵌套 for 循环的情况下编写此代码。我尝试了 ostreambuf_iterator API 如下
copy(m1.begin2(), m1.begin2() + 500 * 400, ostream_iterator<float>(dat, "\n")); // Can only new line everything
但是,如您所见,连续的元素是在新行上编写的,我无法像嵌套 for 循环那样实现排序类型。有没有办法做我在嵌套中使用 STL 算法所做的事情?
【问题讨论】:
标签: c++ c++11 boost stl boost-ublas