简单的 C++ GCC 下的字符串格式化
我正在将其他人的 C++ 库转换为在 GCC 下工作(Xcode)。字符串格式化的基本方法是什么?
编辑:我发现我的问题的原始措辞引起了很多混乱,更不用说一些娱乐了。我需要做的是替换其库中的某些字符串格式化函数。他们已经通过 wxString 完成了格式化,我不想将其包含在我的项目中——我知道 Xcode 中有它,但这对于我需要执行的简单格式化来说似乎是一种大锤式方法。它们具有获取对象并吐出字符串的函数——通常是(char*),我需要创建相同的(char*)输出[然后可能会转到库中的另一个对象;不一定对用户],但以不使用 wxString 的方式进行。我宁愿避免将其转换为 C++。在 Objective-C 中,我总是使用 NSString 的 stringWithFormat: 方法。显然我不想使用任何 NSString 方法,也不想创建 NSString。创建 (char*) 或 C++ 字符串就可以了。不,这不是虚假事实,也不是家庭作业。
I am converting someone else's C++ library to work under GCC (Xcode). What is the basic method for string formatting?
EDIT: I see the original phrasing of my question caused a lot of confusion, not to mention some amusement. What I need to do is replace certain string formatting functions in their library. They have done formatting via wxString, which I don't want to include in my project -- I know it is out there for Xcode, but that seems like a sledgehammer-type approach for the simple formatting I need to do. They have functions which take their objects and spit out strings -- typically (char*), and I need to create the same (char*) outputs [which might then go to another object in the library; not necessarily to the user], but do it in a way that does not use wxString. And I'd rather avoid turning it into C++. In Objective-C, I do this all the time with NSString's stringWithFormat: method. Obviously I don't want to use any NSString methods and don't want to create NSStrings. Creating (char*) or C++ strings would be fine. And no, it's not a false fact, and it's not homework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用流:
You can use streams:
您可以使用 printf 系列函数,但在 99.99% 的情况下,这是有充分理由不被接受的。
看看托尼老虎的回答。
编辑:好的。为什么对格式字符串皱眉。
如何打印自己的类型或来自第三方 C++ 库的类型?与 printf 不同,流是可扩展的:
<代码>cout <<富 << '\n';
对比
哦废话,将非 POD 类型传递给 varargs 会产生未定义的行为;猜猜 foo.name() 的返回类型是什么?有趣的是,它编译得很好,并且根据实现的不同,甚至可能会因为一些奇怪的原因而工作。
过了一段时间,我开始使用自己的非原始
Decimal
实现,由foo.width()/height()/深度()用作返回类型)
。糟糕的是,现在我仅在该行中调用未定义行为 4 次。如何避免格式字符串冗余?我应该像这样外部化打印吗:
float f; fscanf(stdout, "%c %f", f);
->请找出所有错误。You can use the printf family of functions, but in 99.99% of cases, this is heavily frowned upon for very good reason.
See Tony The Tiger's answer.
edit: Okay. Why the frowning upon format strings.
How do you printf your own types or types from some 3rd party C++ library? Unlike printf, streams are extensible:
cout << foo << '\n';
vs
oh crap, passing non-POD types to varargs yields undefined behaviour; guess what
foo.name()
's return type is? Funnily, it compiles fine, and depending on the implementation, might even work for some strange reason.After a while, I begin to use my own non-primitive
Decimal
-implementation, to be used as return-type byfoo.width()/height()/depth()
. Crappily crap, now I invoke undefined behaviour 4 times in that line alone.How do I avoid format-string redundancy? Shall I externalize printing like this:
float f; fscanf(stdout, "%c %f", f);
-> Please find all errors.sprintf()
是一个很好的起点。但是,这实际上取决于您想要实现的目标。std::stringstream
是一个很好的选择,正如 Tony The Tiger 指出的那样, Boost.Format 是另一个。sprintf()
is a good starting point. However, it really depends on what you wan't to achieve.std::stringstream
is a good alternative as Tony The Tiger points out, Boost.Format is another.在 C++ 中有很多方法可以做到这一点。例如:
您还可以直接打印到某些输出流,使用C++ I/O流,甚至达到Boost.LexicalCast。
There are many ways of doing it in C++. For example:
You can also print directly to some output stream, use C++ I/O streams, even to the degree of Boost.LexicalCast.