C ++内联字符串格式和数字转换

时间:2020-12-29 00:06:12

C# has a nice static method

C#有一个很好的静态方法

String.Format(string, params string[]);

that returns a new string with the formatting and values that are provided. Is there an equivalent in C++?

返回带有提供的格式和值的新字符串。 C ++中有等价的吗?

The reason is because I'm using log4cxx and want to take advantage of the macros like

原因是因为我正在使用log4cxx并希望利用像这样的宏

LOG4CXX_DEBUG( logger, expr );

that uses short-circuit evaluation so that expr is never evaluated if the log level DEBUG is not enabled.

使用短路评估,以便在未启用日志级别DEBUG时永远不会评估expr。

Currently, in C++, I do it like this:

目前,在C ++中,我这样做:

CString msg;
msg.Format( formatString, values... );

LOG4CXX_INFO( _logger, msg );

which is defeating the purpose since I have to allocate and format the string first so there isn't nearly as efficiency coming out of the short-circuit logic.

由于我必须首先分配和格式化字符串,因此短路逻辑的效率几乎没有。

There's a similar problem when trying to do simple logging with numerica values. This, won't compile:

尝试使用numerica值进行简单日志记录时会出现类似问题。这个,不会编译:

LOG4CXX_DEBUG( _logger, "the price is " + _some-double_);

So I end up having to write something like this:

所以我最终不得不写这样的东西:

CString asStr;
asStr.Format( "%d", _some-double_ );
LOG4CXX_DEBUG( _logger, "the price is " + asStr );

which again defeats the puporse.

这又击败了傀儡。

I'm not at all a C++ expert so I'm hoping more knowledgeable people can help.

我不是一个C ++专家,所以我希望更多知识渊博的人可以提供帮助。

Thanks in advance!

提前致谢!

5 个解决方案

#1


9  

log4cxx accepts stream like parameters, so you can write, for example:

log4cxx接受类似参数的流,因此您可以编写,例如:

LOG4CXX_DEBUG( _logger, "the price is " << price );

#2


4  

You can fall back to C and use sprintf

您可以回退到C并使用sprintf

printf(stderr,"The Error(%d) happened(%s)\n",error,errmsg(error));

Boost also has a format.

Boost也有一种格式。

// iostream with boost::format
std::cerr << boost::format("The Error(%d) happened(%s)\n") % error % errmsg(error);

If you want to shortcut

如果你想快捷方式

logger && (std::cerr << Stuff); // Where Stuff can be your boost::format

#3


3  

Using the standard libraries, there is no way to produce a formatted string without some type of memory allocation on your part. The C++ string class does not have a "format" function per se, so you must use a stringstream object in order to concatenate numbers with text, but that would involve allocating the object. Looking at C functions like sprintf, you need to allocate a char array beforehand since sprintf does not allocate any memory itself.

使用标准库,如果没有某种类型的内存分配,就无法生成格式化的字符串。 C ++字符串类本身没有“格式”函数,因此必须使用stringstream对象才能将数字与文本连接起来,但这将涉及分配对象。看看像sprintf这样的C函数,你需要预先分配一个char数组,因为sprintf本身不会分配任何内存。

That said, even if there existed a static function such as "string::format" I doubt you would get much of a speed advantage over allocating a stringstream object yourself and manipulating it, given that the static function would most likely be doing the same things in the background in any event.

也就是说,即使存在诸如“string :: format”之类的静态函数,我怀疑你会比自己分配一个字符串流对象并操纵它有更大的速度优势,因为静态函数很可能会做同样的事情。在任何情况下背景中的事情。

#4


3  

My favorite way to do inline formating is with the boost.format library. For example:

我最喜欢的内联格式化方法是使用boost.format库。例如:

#include <boost/format.hpp>
using namespace boost;

LOG4CXX_INFO( _logger, str(format("cheese it %i, %g") % 1234 % 1.3) );

It's very handy for using variable arguments in logging and macro functions.

在日志记录和宏函数中使用变量参数非常方便。

#5


2  

Either use the boost format library or else handcode your own little version (like make_string here).

要么使用boost格式库,要么手动编码自己的小版本(如make_string here)。

#1


9  

log4cxx accepts stream like parameters, so you can write, for example:

log4cxx接受类似参数的流,因此您可以编写,例如:

LOG4CXX_DEBUG( _logger, "the price is " << price );

#2


4  

You can fall back to C and use sprintf

您可以回退到C并使用sprintf

printf(stderr,"The Error(%d) happened(%s)\n",error,errmsg(error));

Boost also has a format.

Boost也有一种格式。

// iostream with boost::format
std::cerr << boost::format("The Error(%d) happened(%s)\n") % error % errmsg(error);

If you want to shortcut

如果你想快捷方式

logger && (std::cerr << Stuff); // Where Stuff can be your boost::format

#3


3  

Using the standard libraries, there is no way to produce a formatted string without some type of memory allocation on your part. The C++ string class does not have a "format" function per se, so you must use a stringstream object in order to concatenate numbers with text, but that would involve allocating the object. Looking at C functions like sprintf, you need to allocate a char array beforehand since sprintf does not allocate any memory itself.

使用标准库,如果没有某种类型的内存分配,就无法生成格式化的字符串。 C ++字符串类本身没有“格式”函数,因此必须使用stringstream对象才能将数字与文本连接起来,但这将涉及分配对象。看看像sprintf这样的C函数,你需要预先分配一个char数组,因为sprintf本身不会分配任何内存。

That said, even if there existed a static function such as "string::format" I doubt you would get much of a speed advantage over allocating a stringstream object yourself and manipulating it, given that the static function would most likely be doing the same things in the background in any event.

也就是说,即使存在诸如“string :: format”之类的静态函数,我怀疑你会比自己分配一个字符串流对象并操纵它有更大的速度优势,因为静态函数很可能会做同样的事情。在任何情况下背景中的事情。

#4


3  

My favorite way to do inline formating is with the boost.format library. For example:

我最喜欢的内联格式化方法是使用boost.format库。例如:

#include <boost/format.hpp>
using namespace boost;

LOG4CXX_INFO( _logger, str(format("cheese it %i, %g") % 1234 % 1.3) );

It's very handy for using variable arguments in logging and macro functions.

在日志记录和宏函数中使用变量参数非常方便。

#5


2  

Either use the boost format library or else handcode your own little version (like make_string here).

要么使用boost格式库,要么手动编码自己的小版本(如make_string here)。