如何在 C++ 中将 long 转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/947621/
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 convert a long to a string in C++?
提问by Arnis Lapsa
How do I convert a long
to a string
in C++?
如何在 C++ 中将long
a转换为 a string
?
采纳答案by Skurmedel
You could use stringstream.
您可以使用字符串流。
#include <sstream>
// ...
std::string number;
std::stringstream strstream;
strstream << 1L;
strstream >> number;
There is usually some proprietary C functions in the standard library for your compiler that does it too. I prefer the more "portable" variants though.
标准库中通常有一些专有的 C 函数供您的编译器使用。不过,我更喜欢更“便携”的变体。
The C way to do it would be with sprintf, but that is not very secure. In some libraries there is new versions like sprintf_s which protects against buffer overruns.
C 方法是使用 sprintf,但这不是很安全。在一些库中有新版本,如 sprintf_s 可以防止缓冲区溢出。
回答by jichi
In C++11, there are actually std::to_string and std::to_wstring functions in <string>.
在 C++11 中,<string> 中实际上有 std::to_string 和 std::to_wstring 函数。
string to_string(int val);
string to_string(long val);
string to_string(long long val);
string to_string(unsigned val);
string to_string(unsigned long val);
string to_string(unsigned long long val);
string to_string(float val);
string to_string(double val);
string to_string (long double val);
回答by Gordon Freeman
Well if you are fan of copy-paste, here it is:
好吧,如果你喜欢复制粘贴,这里是:
#include <sstream>
template <class T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
回答by Piotr Findeisen
boost::lexical_cast<std::string>(my_long)
more here http://www.boost.org/doc/libs/1_39_0/libs/conversion/lexical_cast.htm
boost::lexical_cast<std::string>(my_long)
更多在这里http://www.boost.org/doc/libs/1_39_0/libs/conversion/lexical_cast.htm
回答by Yochai Timmer
You can use std::to_stringin C++11
您可以在 C++11 中使用std::to_string
long val = 12345;
std::string my_val = std::to_string(val);
回答by dreadwail
int main()
{
long mylong = 123456789;
string mystring;
stringstream mystream;
mystream << mylong;
mystring = mystream.str();
cout << mystring << "\n";
return 0;
}
回答by beef2k
I don't know what kind of homework this is, but most probably the teacher doesn't want an answer where you just call a "magical" existing function (even though that's the recommended way to do it), but he wants to see if you can implement this by your own.
我不知道这是什么类型的作业,但很可能老师不想要一个你只调用“神奇”现有函数的答案(即使这是推荐的方法),但他想看看如果你可以自己实现这个。
Back in the days, my teacher used to say something like "I want to see if you can program by yourself, not if you can find it in the system." Well, how wrong he was ;) ..
以前,我的老师常说“我想看看你能不能自己编程,而不是你能不能在系统中找到它”。好吧,他错了;) ..
Anyway, if your teacher is the same, here is the hard wayto do it..
无论如何,如果你的老师是一样的,这是很难做到的。
std::string LongToString(long value)
{
std::string output;
std::string sign;
if(value < 0)
{
sign + "-";
value = -value;
}
while(output.empty() || (value > 0))
{
output.push_front(value % 10 + '0')
value /= 10;
}
return sign + output;
}
You could argue that using std::string
is not "the hard way", but I guess what counts in the actual agorithm.
您可能会争辩说使用std::string
不是“困难的方式”,但我想在实际算法中很重要。
回答by Fred Larson
There are several ways. Read The String Formatters of Manor Farmfor an in-depth comparison.
有几种方法。阅读Manor Farm 的字符串格式化程序进行深入比较。
回答by Martin Beckett
#include <sstream>
....
std::stringstream ss;
ss << a_long_int; // or any other type
std::string result=ss.str(); // use .str() to get a string back
回答by Don
Check out std::stringstream
.
退房std::stringstream
。