C ++运行时字符串格式化 [英] C++ Runtime string formatting
问题描述
通常我使用流来格式化的东西,但在这种情况下?我不知道格式,直到运行时。
Usually I use streams for formatting stuff however in this case ?I don't know the format until runtime.
我想要能够采取像以下格式字符串:
Hello {0}!您最后一次登录的时间是{1,日期:dd / mm / yy}。
...并在变量Fire Lancer和1247859223中提供,最后使用以下格式化字符串: br />
Hello Fire Lancer!您最后一次登录的时间是17/07/09。
I want to be able to take something like the following format string:
Hello {0}! Your last login was on {1,date:dd/mm/yy}.
...and feed in the variables "Fire Lancer" and 1247859223, and end up with the following formatted string:
Hello Fire Lancer! Your last login was on 17/07/09.
在其他语言中,我使用内置的支持这种东西,例如pythons格式字符串方法, c ++似乎没有任何这样的功能,接受不是很安全的C打印方法。
In other languages I use there is built in support for this kind of thing, eg pythons format string method, however in c++ there doesn't seem to be any such functionality, accept the C print methods which are not very safe.
这也是一个高性能的程序,所以任何解决方案我使用需要解析格式字符串一次并存储它(例如mayby一个Parse方法返回FormatString对象与格式(字符串)方法),不重新解析字符串每次格式化方法被调用...
Also this is for a high performance program, so whatever solution I use needs to parse the format string once and store it (eg mayby a Parse method that returns a FormatString object with a Format(string) method), not reparse the string every time the format method is called...
推荐答案
Boost格式化为您:
Boost Formatting does that for you:
http://www.boost.org/doc/libs/1_39_0/libs/format/ doc / format.html
Check out this question and answer for examples of usage:
这篇关于C ++运行时字符串格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!