简体   繁体   English

C ++ ostream格式

[英]C++ ostream formatting

I want to get a C++ class Xyz such that the following: 我想要一个C ++类Xyz ,如下所示:

Xyz mything("/tmp/x1");

int main(int argc, char **argv) {
    mything << "Hello world";
}

causes a file "/tmp/x1" to be created, and the following 导致创建文件“ / tmp / x1”,然后执行以下操作

2013-12-04 12:01 :: Hello world 2013-12-04 12:01 ::你好世界

to be written to it. 写给它。

The first issue I have is that ostream has many specialised methods for each type that may follow the << . 我遇到的第一个问题是ostream对于每种可能在<<类型都有很多专门的方法。 The second is that if I have 第二个是如果我有

cout << "hello" << "world";

and

cout << "hello";

then the implementation for operator << called for "hello" needs to know whether anything follows it in the statement (how do I effectively do an if(last_in_line_of_<<) type construct?). 那么operator <<的实现被称为"hello"需要知道语句中是否有任何内容(我该如何有效地进行if(last_in_line_of_<<)类型的构造?)。 The standard cout does this by having objects like endl . 标准cout通过具有endl对象来实现此目的。

Basically I want to create a global log object with a single line, once the class has been defined, and to write to it with ostream operations such that the string that, say, cout would get would be wrapped in a larger string. 基本上,一旦定义了类,我想用一行创建一个全局日志对象,并使用ostream操作对其进行写入,以便将cout得到的字符串包裹在一个较大的字符串中。

I've ended up looking at this way of doing it since the varargs way is painful and many people seem to recommend against it. 由于varargs方法很痛苦,而且很多人似乎都建议不要这样做,所以我最终还是考虑采用这种方法。

In scripting languages like Python and Ruby, objects have a str or to_s method (or equivalent) and print and format call these to format them, so that "hello {} world".format(x) formats one parameter, and "hello {} world".format(" ".join(x)) joins a list, after formatting to strings, with spaces and then sticks that into the resulting string. 在像Python和Ruby这样的脚本语言中,对象具有strto_s方法(或等效方法),并对其进行printformat以对其进行格式化,从而使"hello {} world".format(x)格式化一个参数,而"hello {} world".format(" ".join(x))在格式化为字符串后,使用空格加入列表,然后将其"hello {} world".format(" ".join(x))到结果字符串中。

I'm wondering what the right C++ way is to do this. 我想知道什么是正确的C ++方法。

Instad to giving to Xyz a stream behavior, make it a manipulator and use normal streams facilities: 可以向Xyz提供流行为,使其成为操纵器并使用常规流功能:

A function prototyped as 原型为

std::ostream& logtime(std::ostream& s)
{
   /* write date and time to s */
   return s;
}

can be used as 可以用作

std::cout << logtime << "text and other things" << std::endl;

if std::cout is replaced by whatever other ostream (like a outfile variable of type std::filestream ) you get exactly that effect, but using a manipulator that can work with whatever type of output stream (non necessarily a file) 如果将std :: cout替换为其他任何ostream (例如,类型为std::filestreamoutfile变量),则您将获得完全相同的效果,但使用的操纵器可以处理任何类型的输出流(不一定是文件)

std::ofstream outfile("file");
outfile << logtime << "text and whatever else" << std::endl;

To learn how to format your output , see iomanip in the Standard C++ Library reference at cplusplus.com. 要了解如何格式化输出 ,请参阅cplusplus.com上“ 标准C ++库”参考中的iomanip。

For example: 例如:

  • setfill -- Set fill character setfill-设置填充字符
  • setprecision -- Set decimal precision setprecision-设置小数精度
  • setw -- Set field width setw-设置字段宽度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM