C++ 如何从 DWORD 构造 std::string?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/893670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I construct a std::string from a DWORD?
提问by Johannes Schaub - litb
I have following code:
我有以下代码:
Tools::Logger.Log(string(GetLastError()), Error);
GetLastError()
returns a DWORD
a numeric value, but the constructor of std::string
doesn't accept a DWORD
.
GetLastError()
返回一个DWORD
数值,但 的构造函数std::string
不接受DWORD
。
What can I do?
我能做什么?
回答by Doug T.
You want to read up on ostringstream:
您想阅读 ostringstream:
#include <sstream>
#include <string>
int main()
{
std::ostringstream stream;
int i = 5;
stream << i;
std::string str = stream.str();
}
回答by Johannes Schaub - litb
You want to convert the number to a string
:
您想将数字转换为 a string
:
std::ostringstream os;
os << GetLastError();
Log(os.str(), Error);
Or boost::lexical_cast
:
或者boost::lexical_cast
:
Log(boost::lexical_cast<std::string>(GetLastError()), Error);
回答by Max Truxa
Since C++11
从 C++11 开始
std::to_string()
with overloads for int
, long
, long long
, unsigned int
, unsigned long
, unsigned long long
, float
, double
, and long double
.
std::to_string()
与重载int
,long
,long long
,unsigned int
,unsigned long
,unsigned long long
,float
,double
,和long double
。
auto i = 1337;
auto si = std::to_string(i); // "1337"
auto f = .1234f;
auto sf = std::to_string(f); // "0.123400"
Yes, I'm a fan of auto
.
是的,我是 的粉丝auto
。
To use your example:
使用您的示例:
Tools::Logger.Log(std::to_string(GetLastError()), Error);
回答by Konrad Rudolph
Use Boost's lexical_cast
for simple cases such as the above:
将 Boostlexical_cast
用于简单的情况,例如上述:
Tools::Logger.Log(lexical_cast<string>(GetLastError()), Error);
回答by dcw
You can use STLSoft's winstl::int_to_string(), as follows:
您可以使用STLSoft的winstl::int_to_string(),如下所示:
Tools::Logger.Log(winstl::int_to_string(GetLastError()), Error);
Also, if you want to lookup the string form of the error code, you can use STLSoft's winstl::error_desc.
另外,如果要查找错误代码的字符串形式,可以使用 STLSoft 的winstl::error_desc。
There were a bunch of articles in Dr Dobb'sabout this a few years ago: parts one, two, three, four. Goes into the subject in greatdetail, particularly about performance.
回答by Beno?t
Use std::stringstream.
使用 std::stringstream。
std::stringstream errorStream;
errorStream << GetLastError();
Tools::Logger.Log(errorStream.str(), Error);
回答by Mykola Golubyev
As all guys here suggested, implementation will use stringstream.
In my current project we created function
正如这里的所有人所建议的那样,实现将使用 stringstream。
在我当前的项目中,我们创建了函数
template <typename T>
std::string util::str::build( const T& value );
to create string from any source.
从任何来源创建字符串。
So in our project it would be
所以在我们的项目中
Tools::Logger.Log( util::str::build(GetLastError()) );
Such usage of streams in the suggested way wouldn't pass my review unless someone wrap it.
除非有人包装它,否则以建议的方式使用流不会通过我的。
回答by PiNoYBoY82
what i normally do is:
我通常做的是:
std::ostringstream oss;
oss << GetLastError() << " :: " << Error << std::endl;
Tools::Logger.Log(oss.str()); // or whatever interface is for logging