std :: string和整数 [英] std::string and integers

查看:417
本文介绍了std :: string和整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然以下不起作用:


int i = 5;

std :: string myString =" Number is" + i +请大家祝你好!


所以有人能告诉我这种方式最好的方法吗?

我很有信心我没有使用sprintf。


感谢,

GA

解决方案

< blockquote>Glen Able < SM ************* @ hotMEmail.com>写道:

显然以下不起作用:
int i = 5;
std :: string myString =" Number is" + i +谢谢你,;
所以任何人都可以让我知道这种事情最好的方式是什么?
我确定它没有使用sprintf。




您可以使用cout样式格式打印到字符串。请参阅常见问题
http://www.parashift.com/c++-faq-lit....html#faq-38.1





2004年7月8日星期四,Glen Able写道:

显然以下不起作用:

int i = 5 ;
std :: string myString =" Number is" + i +请你好吗?

所以任何人都可以告诉我这种事情最好的方法是什么?
我确定它没有使用sprintf。

谢谢,
GA



//单程:


#include < sstream>

#include< string>

#include< iostream>

使用命名空间std;


int main()

{

int i = 5;

string myString1 ="数字是 ;

string myString2 ="谢谢,请;


ostringstream os;


os<< myString1<< i<< myString2;

string msgString = os.str();


cout<< msgString<< endl;


}


试试

int i = 5;

std :: string myString;

myString =" Number is" + i +谢谢你,

// ANders

2004年7月8日星期四09:28:58 +0100,Glen Able写道:

显然以下不起作用:

int i = 5;
std :: string myString =" Number is" + i +请你好吗?

所以任何人都可以告诉我这种事情最好的方法是什么?
我确定它没有使用sprintf。

谢谢,
GA




Obviously the following doesn''t work:

int i = 5;
std::string myString = "Number is " + i + " thankyou please";

So can anyone give me some idea what''s the nicest way to this sort of thing?
I''m sure it''s not using sprintf.

with thanks,
G.A.

解决方案

"Glen Able" <sm*************@hotMEmail.com> writes:

Obviously the following doesn''t work: int i = 5;
std::string myString = "Number is " + i + " thankyou please"; So can anyone give me some idea what''s the nicest way to this sort of thing?
I''m sure it''s not using sprintf.



You can use cout-style formatting to print to a string. See the FAQ
http://www.parashift.com/c++-faq-lit....html#faq-38.1




On Thu, 8 Jul 2004, Glen Able wrote:

Obviously the following doesn''t work:

int i = 5;
std::string myString = "Number is " + i + " thankyou please";

So can anyone give me some idea what''s the nicest way to this sort of thing?
I''m sure it''s not using sprintf.

with thanks,
G.A.


// one way:

#include <sstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
int i = 5;
string myString1 = "The number is ";
string myString2 = " thank you, please";

ostringstream os;

os << myString1 << i << myString2;
string msgString = os.str();

cout << msgString << endl;

}


try
int i = 5;
std::string myString;
myString = "Number is " + i + " thankyou please";
// ANders
On Thu, 08 Jul 2004 09:28:58 +0100, Glen Able wrote:

Obviously the following doesn''t work:

int i = 5;
std::string myString = "Number is " + i + " thankyou please";

So can anyone give me some idea what''s the nicest way to this sort of thing?
I''m sure it''s not using sprintf.

with thanks,
G.A.




这篇关于std :: string和整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆