格式化std :: ostream的输出 [英] Formating Output of std::ostream

查看:365
本文介绍了格式化std :: ostream的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想格式化''std :: ostream''的输出,但我不知道如何

这样做[在ansi-c ++]。


示例:

int main()

{

my_out<< 嗨。我是一个 << "电脑。;

my_out<< 很好。;

}


现在我想把它作为输出:

[TIMESTAMP]嗨。

[TIMESTAMP]我是电脑。

[TIMESTAMP]很好。

有什么想法吗?

谢谢

-

Johannes B.

Hi,

I want to format the output of a ''std::ostream'', but i dont know how to
do it [in ansi-c++].

Example:
int main()
{
my_out << "Hi.\nI''am a" << " Computer.";
my_out << "Nice.";
}

Now i want to get this as output:
[TIMESTAMP] Hi.
[TIMESTAMP] I''am a Computer.
[TIMESTAMP] Nice.
Any Ideas?
Thanks
--
Johannes B.

推荐答案

Johannes Barop写道:
Johannes Barop wrote:


我想格式化''std :: ostream''的输出,但我不知道怎么做[在ansi-c ++中] 。

示例:
int main()
{
my_out<< 嗨。我是一个 << "电脑。;
my_out<< 很好。;
}
现在我想把它作为输出:
[TIMESTAMP]嗨。
[TIMESTAMP]我是一个电脑。
[TIMESTAMP]很好。

有什么想法吗?
谢谢
Hi,

I want to format the output of a ''std::ostream'', but i dont know how to
do it [in ansi-c++].

Example:
int main()
{
my_out << "Hi.\nI''am a" << " Computer.";
my_out << "Nice.";
}

Now i want to get this as output:
[TIMESTAMP] Hi.
[TIMESTAMP] I''am a Computer.
[TIMESTAMP] Nice.
Any Ideas?
Thanks



查看dinkumware.com或其他地方为< iostream>操纵者。


Greets,

-

Stephan''hagbard''Grein, < St *********** @ gmail.com>
http://hagbard.ninth-art.de/

GnuPG-Key-ID:0x08FA3507

< ESC> :wq


Look up on dinkumware.com or elsewhere for the manipulators for <iostream>.

Greets,
--
Stephan ''hagbard'' Grein, <St***********@gmail.com>
http://hagbard.ninth-art.de/
GnuPG-Key-ID: 0x08FA3507
<ESC> :wq


Johannes Barop写道:
Johannes Barop wrote:


我想格式化''标准输出:: ostream'',但我不知道怎么做[在ansi-c ++中]。

示例:
int main()
{
my_out<< 嗨。我是一个 << "电脑。;
my_out<< 很好。;
}
现在我想把它作为输出:
[TIMESTAMP]嗨。
[TIMESTAMP]我是一个电脑。
[TIMESTAMP]很好。
Hi,

I want to format the output of a ''std::ostream'', but i dont know how to
do it [in ansi-c++].

Example:
int main()
{
my_out << "Hi.\nI''am a" << " Computer.";
my_out << "Nice.";
}

Now i want to get this as output:
[TIMESTAMP] Hi.
[TIMESTAMP] I''am a Computer.
[TIMESTAMP] Nice.




你似乎想要两件事:每行打印时间戳和

每个陈述的结尾。前者很简单,后者则不容易。


对于新行,你必须解析输出并找到它们自己。

class timestamp_out

{

public:

void print(const std :: string& s )

{

//使用\ n作为分隔符的tokenize s

//参见boost :: tokenizer

//输出每行后面加一个时间戳

}

};


timestamp_out& operator<<(timestamp_out& out,const std :: string& s)

{

out.print(s);

return out;

}


对于这些陈述,我可以从第一个运算符< $
返回代理;<并且当代理被销毁时,该代理在timestamp_out

中设置一个标志。由于这个临时存在直到

结束语,它可能会起作用。


Jonathan



You seem to want two things here: print timestamps on each line AND at
the end of each statement. The former is easy and the latter is not.

For the new lines, you''ll have to parse the output and find them
yourself.

class timestamp_out
{
public:
void print(const std::string& s)
{
// tokenize s using \n as separator
// see boost::tokenizer
// output each line preceded by a timestamp
}
};

timestamp_out& operator<<(timestamp_out& out, const std::string& s)
{
out.print(s);
return out;
}

For the statements, on top of my head, you could return a proxy from
the first operator<< and have that proxy set a flag in timestamp_out
when it is destroyed. Since this temporary will exist until the end of
the statement, it will probably work.

Jonathan


Jonathan Mcdougall schrieb:
Jonathan Mcdougall schrieb:
示例:
int main()
{
my_out<< ; 嗨。我是一个 << "电脑。;
my_out<< 很好。;
}
现在我想把它作为输出:
[TIMESTAMP]嗨。
[TIMESTAMP]我是一个计算机。
[TIMESTAMP]很好。
Example:
int main()
{
my_out << "Hi.\nI''am a" << " Computer.";
my_out << "Nice.";
}

Now i want to get this as output:
[TIMESTAMP] Hi.
[TIMESTAMP] I''am a Computer.
[TIMESTAMP] Nice.


您似乎想要两件事:在每一行打印时间戳,并在每个语句的末尾打印。前者很容易而后者则不容易。


Yeha。第二个是我的问题:/


对于这些陈述,我可以从第一个运算符<<<<<并且当代理被销毁时,该代理在timestamp_out中设置一个标志。由于这个临时存在直到声明结束,它可能会起作用。

You seem to want two things here: print timestamps on each line AND at
the end of each statement. The former is easy and the latter is not.
Yeha. The second one is my problem :/

For the statements, on top of my head, you could return a proxy from
the first operator<< and have that proxy set a flag in timestamp_out
when it is destroyed. Since this temporary will exist until the end of
the statement, it will probably work.




Hm,你可以向我解释更多细节吗?我不适合使用C ++


-

Johannes B.



Hm, can u explain it to me with more detail? I''am not that fit in C++

--
Johannes B.


这篇关于格式化std :: ostream的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆