string C++ 将 int 转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15375995/
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
C++ converting an int to a string
提问by Erk
I know this is extremely basic but I'm very new to C++ and can't seem to find an answer. I'm simply trying to convert a few integers to strings. This method works:
我知道这是非常基本的,但我对 C++ 很陌生,似乎找不到答案。我只是想将一些整数转换为字符串。这种方法有效:
int a = 10;
stringstream ss;
ss << a;
string str = ss.str();
but when I need to convert the second and third ones like this:
但是当我需要像这样转换第二个和第三个时:
int b = 13;
stringstream ss;
ss << a;
string str2 = ss.str();
int c = 15;
stringstream ss;
ss << b;
string str3 = ss.str();
I get this error:
我收到此错误:
'std::stringstream ss' previously declared here
Do I need to somehow close stringstream? I've noticed that if I put them far away from each other in my code the compiler doesn't mind but that doesn't seem like something I should do. Does anyone have suggestions?
我需要以某种方式关闭 stringstream 吗?我注意到,如果我在代码中将它们彼此远离,编译器不会介意,但这似乎不是我应该做的事情。有没有人有建议?
回答by Bdloul
You're trying to redeclare the same stringstream with the same name. You can modify your code like this in order to work :
您正在尝试使用相同的名称重新声明相同的字符串流。您可以像这样修改代码以使其工作:
int b = 13;
stringstream ss2;
ss2 << a;
string str2 = ss2.str();
Or like this if you don't want to redeclare it :
或者,如果您不想重新声明它,则像这样:
int b = 13;
ss.str(""); // empty the stringstream
ss.clear();
ss << a;
string str2 = ss.str();
You can also use , which is much quicker :
您还可以使用 ,这要快得多:
int c = 42;
std::string s = std::to_string(c);
回答by Potatoswatter
The stringstream
is a variable just like any other. To make the idiom work twice without change, put it in braces:
这stringstream
是一个变量,就像任何其他变量一样。要使习语两次不变地工作,请将其放在大括号中:
int a = 13;
string str2;
{
stringstream ss;
ss << a;
str2 = ss.str();
} // ss ceases to exist at the closing brace
int b = 15;
string str3;
{
stringstream ss; // now we can make a new ss
ss << b;
str3 = ss.str();
}
Since C++11, you can do
从 C++11 开始,你可以做
std::string str4 = std::to_string( b );
or (with c
of arbitrary type)
或(具有c
任意类型)
std::string str5 = ( std::stringstream() << c ).str();
There are other solutions, of course.
当然,还有其他解决方案。
回答by d.moncada
try
尝试
#include <string>
int a = 10;
std::string s = std::to_string(a);
回答by Mike Dinescu
The particular error you are getting is telling you that you can't declare two variables with the same name (ss
in your case) within the same scope.
您遇到的特定错误是告诉您不能ss
在同一范围内声明两个具有相同名称的变量(在您的情况下)。
If you wanted to create a new stringstream
you could call it something else.
如果你想创建一个新的,stringstream
你可以称之为别的东西。
But there are other ways to convert an int to a string.. you could use std::to_string().
但是还有其他方法可以将 int 转换为字符串..您可以使用std::to_string()。
回答by Joe
Make sure you declare:
确保您声明:
using namespace std;
Then insert the code:
然后插入代码:
int a = 10;
ostringstream ssa;
ssa << a;
string str = ssa.str();
int b = 13;
ostringstream ssb;
ssb << b;
string str2 = ssb.str();
int c = 15;
ostringstream ssc;
ssc << c;
string str3 = ssc.str();
回答by 4aRk Kn1gh7
One of the simplest way i can think of doing it is:
我能想到的最简单的方法之一是:
string str = string(itoa(num));