从printf到C ++格式 [英] From printf to C++ formatting

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

问题描述

好的,我收回了我说的关于printf的令人讨厌的东西。至少在我看到使用C ++格式化程序写这个的同样简洁的方式之前。我想

将以下内容转换成我可以放到C ++ std :: ostream上的东西:


printf(" [%2x]%-20s %-8.8s%08x%06x%02x%-3.3s%02x%04x%02x \ n",

i,

std :: string(pSec-> ; GetName())。sububtr(0,20).c_str(),

SectionTypes(pSec-> GetType())。c_str(),

pSec- > GetAddress(),

pSec-> GetSize(),

pSec-> GetEntrySize(),

SectionFlags(pSec- > GetFlags())。c_str(),

pSec-> GetLink(),

pSec-> GetInfo(),

pSec-> GetAddrAlign());


我看过的一个选项,可能会回来,是Boost.Format

图书馆。现在,我想看看我是否可以使用C ++格式的操纵器找到一个相当优雅的方式来获得
。有没有人对这种情况采取系统的方法



-

如果我们的假设是关于任何事情而不是关于某一个或更多

特定的东西,然后我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell

OK, I take back the nasty stuff I said about printf. At least until I see a
similarly concise way of writing this using C++ formatters. I want to
convert the following to something I can put onto a C++ std::ostream:

printf( " [%2x] %-20s %-8.8s %08x %06x %02x %-3.3s %02x %04x %02x\n",
i,
std::string( pSec->GetName() ).substr( 0, 20 ).c_str(),
SectionTypes( pSec->GetType() ).c_str(),
pSec->GetAddress(),
pSec->GetSize(),
pSec->GetEntrySize(),
SectionFlags( pSec->GetFlags() ).c_str(),
pSec->GetLink(),
pSec->GetInfo(),
pSec->GetAddrAlign() );

One option I''ve looked at, and may come back to, is the Boost.Format
library. For now, I want to see if I can find a reasonably elegant way of
using the C++ format manipulators. Does anybody have a systematic approach
to this kind of situation?
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell

推荐答案



Steven T. Hatton写道:

Steven T. Hatton wrote:
好的,我收回了我说过的令人讨厌的东西关于printf。至少在我看到使用C ++格式化程序编写这种类似的简洁方法之前。我想将以下内容转换成我可以放到C ++ std :: ostream上的东西:

printf(" [%2x]%-20s%-8.8s%08x%06x %02x%-3.3s%02x%04x%02x \ n",
我,
std :: string(pSec-> GetName())。substr(0,20).c_str( ),
SectionTypes(pSec-> GetType())。c_str(),
pSec-> GetAddress(),
pSec-> GetSize(),
pSec - > GetEntrySize(),
SectionFlags(pSec-> GetFlags())。c_str(),
pSec-> GetLink(),
pSec-> GetInfo(),
pSec-> GetAddrAlign());

我看过的一个选项是Boost.Format
库。现在,我想看看我是否能找到一种使用C ++格式操纵器的相当优雅的方式。有没有人对这种情况采取系统的方法?
OK, I take back the nasty stuff I said about printf. At least until I see a
similarly concise way of writing this using C++ formatters. I want to
convert the following to something I can put onto a C++ std::ostream:

printf( " [%2x] %-20s %-8.8s %08x %06x %02x %-3.3s %02x %04x %02x\n",
i,
std::string( pSec->GetName() ).substr( 0, 20 ).c_str(),
SectionTypes( pSec->GetType() ).c_str(),
pSec->GetAddress(),
pSec->GetSize(),
pSec->GetEntrySize(),
SectionFlags( pSec->GetFlags() ).c_str(),
pSec->GetLink(),
pSec->GetInfo(),
pSec->GetAddrAlign() );

One option I''ve looked at, and may come back to, is the Boost.Format
library. For now, I want to see if I can find a reasonably elegant way of
using the C++ format manipulators. Does anybody have a systematic approach
to this kind of situation?




使用C ++ io操纵器它看起来很丑陋而且更容易出错

然后是一个简单的格式字符串。 printf的唯一问题是,它不是类型安全的,它需要经验才能知道它的陷阱并安全使用



C ++标准流和格式化非常笨拙。



With C++ io manipulators it would look ugly and much more error-prone
then a simple format string. The only trouble with printf is that it''s
not type safe and it requires experience to know its pitfalls and use
it safely.

C++ standard streams and formatting are really clumsy.




Maxim Yegorushkin写道:

Maxim Yegorushkin wrote:
使用C ++ io操纵器,它看起来很丑陋,更容易出错,然后是一个简单的格式字符串。 printf的唯一问题是它不是类型安全的,它需要经验才能知道它的陷阱并安全地使用它。

C ++标准流和格式化确实是笨拙。
With C++ io manipulators it would look ugly and much more error-prone
then a simple format string. The only trouble with printf is that it''s
not type safe and it requires experience to know its pitfalls and use
it safely.

C++ standard streams and formatting are really clumsy.




这就是为什么你可以使用boost :: format,它具有printf

的优点,并使用printf格式,但是还没有缺乏类型安全性和可扩展性的缺点。 (扩展类型为流b / b
,而不是可以给出格式化代码的数据类型)。


(你总是可以扩展你自己的对象如果需要,可以生成不同格式的字符串。



That''s why you can use boost::format which has the advantages of printf
and uses printf formatting, but also doesn''t have the disadvantages of
the lack of type-safety and extensibility. (Extensibility in the type
of streams, not the type of data that can be given a formatting code).

(You can always extend your own objects to produce strings, of
different formats if required).




Steven T. Hatton写道:

Steven T. Hatton wrote:
好吧,我收回了我所说的关于printf的令人讨厌的东西。至少在我看到使用C ++格式化程序编写这种类似的简洁方法之前。我想将以下内容转换成我可以放到C ++ std :: ostream上的东西:

printf(" [%2x]%-20s%-8.8s%08x%06x %02x%-3.3s%02x%04x%02x \ n",
我,
std :: string(pSec-> GetName())。substr(0,20).c_str( ),
SectionTypes(pSec-> GetType())。c_str(),
pSec-> GetAddress(),
pSec-> GetSize(),
pSec - > GetEntrySize(),
SectionFlags(pSec-> GetFlags())。c_str(),
pSec-> GetLink(),
pSec-> GetInfo(),
pSec-> GetAddrAlign());

我看过的一个选项是Boost.Format
库。现在,我想看看我是否能找到一种使用C ++格式操纵器的相当优雅的方式。有没有人对这种情况采取系统的方法?
OK, I take back the nasty stuff I said about printf. At least until I see a
similarly concise way of writing this using C++ formatters. I want to
convert the following to something I can put onto a C++ std::ostream:

printf( " [%2x] %-20s %-8.8s %08x %06x %02x %-3.3s %02x %04x %02x\n",
i,
std::string( pSec->GetName() ).substr( 0, 20 ).c_str(),
SectionTypes( pSec->GetType() ).c_str(),
pSec->GetAddress(),
pSec->GetSize(),
pSec->GetEntrySize(),
SectionFlags( pSec->GetFlags() ).c_str(),
pSec->GetLink(),
pSec->GetInfo(),
pSec->GetAddrAlign() );

One option I''ve looked at, and may come back to, is the Boost.Format
library. For now, I want to see if I can find a reasonably elegant way of
using the C++ format manipulators. Does anybody have a systematic approach
to this kind of situation?




在这个特定的例子中,你有很多对pSec的调用。现在

你可能想要保持这个免费功能而不是会员

功能,也许这个课程不是你自己的。


但是它不灵活,因为你只能输出到控制台或

,无论控制台被重定向到哪里。你也总是要输出这种特殊格式的b $ b输出或者你要修改它吗?并且

记住类型安全 - 例如,如果您允许用户选择他们的

自己的格式字符串,以便他们可以以不同的格式输出,并且他们的

格式的字符串不符合你的功能,那么它仍然可以很好地编译,但可以bang!当你运行它时。


pSec显然是指向某个东西的指针,它不能为NULL,并且

大概它只能读取和类它读取是const-correct

所以我会从指针改为const引用。


接下来,如果你真的觉得你必须输出然后使用sprintf - 至少

这里你知道你需要多大的缓冲区,那么你可以得到你的

函数来返回你构造的转换为

std :: string。 (但我会使用boost :: format)。


或者,你的功能可以采用ostream和amp;参数和

返回它。如果用户想要一个字符串,他们将使用ostringstream。如果

这是你打算格式化对象的唯一方法那么你

也可以这样做


std :: ostream& operator<<(std :: ostream&,const Section&);


假设Section是你班级的名字。 (作为第一个元素我可能是不相关的,你可以单独传输它。)



In this particular instance though you have a lot of calls to pSec. Now
you might want to keep this a free function rather than a member
function, and maybe the class is not your own.

However it is inflexible as you can only output to the console or
wherever the console is redirected to. Also are you always going to
output to this particular format or are you going to modify it? And
remember type-safety - if, for example, you allow users to pick their
own format string so they can output in different formats, and their
format string doesn''t comply with your function, then it will still
compile beautifully but could go "bang!" when you run it.

pSec is obviously a pointer to something, and it can''t be NULL, and
presumably it only reads and the class it reads from is const-correct
so I''d change from a pointer to a const reference.

Next, if you really feel you must output then use sprintf - at least
here you know how big a buffer you need, so then you can get your
function to return what you have constructed converting it to a
std::string. (But I would go with boost::format).

Alternatively, your function could take an ostream& parameter and
return it. If the user wants a string they will use ostringstream. If
this is the only way you are ever going to format the object then you
may as well make this

std::ostream & operator<<( std::ostream &, const Section & );

Assuming Section is the name of your class. (As the first element i is
probably unrelated you would stream that in separately).


这篇关于从printf到C ++格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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