您的位置:首页 > 其它

string 和 int 之间的相互转化

2017-02-26 14:40 381 查看
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());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: