将输出写入文件的不同列
Writing output to different columns of a file
嗨,我想知道是否有任何函数库可以让我方便地做到这一点,或者如果你有任何建议,我如何才能优雅地做到这一点,而不需要编写一行又一行的代码(这就是我最终用ofstream做的)。
Loop over i
Loop over j
Evaluate f(i*alpha,j);
Write f to column i;
就像这样。我需要比较一个QM问题的100个不同的特征函数,我宁愿不为每个值写一个文件,也会使绘图更容易。
我做了谷歌没有任何可用的结果,感谢任何帮助:)
你的问题是对齐的,表格输出,即视觉上有列,或者能够写,例如第4个字段,之后第二个?
对于前者:Minimal c++ solution使用<iomanip>
.
假设你有:
#include <iostream>
#include <vector>
#include <iomanip>
struct Date { int year, month, day;
Date(int year, int month, int day)
: year(year), month(month), day(day) {}
};
struct Time { int hour, minute, second;
Time (int hour, int minute, int second)
: hour(hour), minute(minute), second(second){}
};
struct Birthday { Date date;
Time time;
Birthday (Date date, Time time) : date(date), time(time) {}
};
std::ostream& operator<< (std::ostream &ofs, Time const &rhs) {
using std::setw;
return ofs << std::setfill('0')
<< setw(2) << rhs.hour << ':'
<< setw(2) << rhs.minute << ':'
<< setw(2) << rhs.second;
}
std::ostream& operator<< (std::ostream &ofs, Date const &rhs) {
using std::setw;
return ofs << std::setfill('0')
<< setw(4) << rhs.year << '-'
<< setw(2) << rhs.month << '-'
<< setw(2) << rhs.day;
}
std::ostream& operator<< (std::ostream &ofs, Birthday const &rhs) {
return ofs << rhs.date << ' ' << rhs.time;
}
struct Dude {
std::string first_name;
std::string last_name;
Birthday birthday;
Dude (std::string const &f, std::string const &l, Birthday const &b)
: first_name(f), last_name(l), birthday(b) {}
};
然后你可以输出一个简单的表,像这样:
int main () {
using std::setw;
std::vector<Dude> d;
d.push_back (Dude("John", "Doe", Birthday(Date(1980,12,11),Time(6,45,0))));
d.push_back (Dude("Max", "Mustermann", Birthday(Date(1980,12,11),Time(6,45,0))));
std::cout << std::left;
// Output a fancy header.
std::cout << std::setfill(' ')
<< setw(24) << "<last name>" << "| "
<< setw(16) << "<first name>" << "| "
<< "birthday" << 'n';
// Data output follows. Note: No lines of lines and code.
for (std::vector<Dude>::iterator it=d.begin(), end=d.end(); it!=end; ++it) {
std::cout << std::setfill(' ')
<< setw(24) << it->last_name << "| "
<< setw(16) << it->first_name << "| "
<< it->birthday << 'n';
}
}
如果您的算法允许您这样做,请重构循环以按行编写:
Loop over j {
Loop over i {
Evaluate f(i*alpha, j);
Write f to column i, <TAB>;
}
Write <CR><LF>
}
相关文章:
- 如何在 的开头<x>和结尾<y>之间更改带有文件输出的字符串的值
- C++对象数组的文件输出
- bcdedit 文件输出不起作用,但其他命令可以
- 如何从文本文件输出数据?(学生成绩单计划)
- 使用 C++文件 I/O 输出 pdf 文件
- C 文件输出垃圾
- 从堆栈中读取字符后,如何修复不正确的文件输出
- C++:具有随机名称的文件输出
- 在命令提示符下看到换行符,但在文件输出中可以看到 相同的字符串
- 生成文件不会生成预期的文件输出
- C++11 fgetc 在"r+b"模式下使用时向我的文件输出 0
- 使用函数从文件输出
- GCC.obj文件输出不具有确定性(.debug_info,PROGPITS部分)
- C cstdiofile :: Writestring给出了不正确的文件输出
- C 输出:STD :: COUT和文件输出没有相同的内容
- GCC和MSVC之间的文件输出不一致
- C++文件输出奇怪的数字,并且部分代码没有运行
- 如何将特定字符从TXT文件输出到特定点
- C 调用从TXT文件输出信息的函数
- 将交易从文本文件输出到屏幕