请问大神,C++中怎么把int型的值赋给string型的?谢了~~

2024-12-05 12:53:09
推荐回答(5个)
回答1:

#include
#include
using namespace std;
void main()
{
int ii;
string ss;
cin>>ii; /////输入一串数字654
///////////////
char ch[10];
itoa(ii,ch,10);
ss.assign(ch);
////////////////
cout<
}

回答2:

#include
#include
using namespace std;

int main()
{
string str;
char ch[100];
int a = 24;
sprintf(ch,"%d",a);
str = ch;
cout< return 0;
}
string没有直接把数字写进字符串里的,这儿只有先写在char数组里

回答3:

#include
#include
#include
using namespace std;
int main()
{
int ii = 0;
string ss;
cin >> ii;
stringstream stream;
stream << ii;
stream >> ss;
cout << ss << endl;
return 0;
}

回答4:

ss=(string)ii;

回答5:

int a = 10;
std::string str;
str = std::to_string(a);
cout << str << endl;