C++11有这两个方法
#include<string>
#include<iostream>
using namespace std;
int main()
{
int i = 42;
string s = to_string(i);
cout << s << endl;
string sint = "56";
int n = stoi(sint);
cout << n << endl;
return 0;
}
也可以这样
#include "stdafx.h"
#include <string>
#include <sstream>
using namespace std;
void main()
{
// int 转 string
stringstream ss;
int n = 123;
string str;
ss<<n;
ss>>str;
// string 转 int
str = "456";
n = atoi(str.c_str());
}
本文介绍了在C++11中如何使用标准库函数将整数转换为字符串(to_string),以及如何将字符串转换回整数(stoi)。此外还展示了使用stringstream进行类型转换的方法。


被折叠的 条评论
为什么被折叠?



