您的位置:首页 > 编程语言 > C语言/C++

C++ int与string相互转换

2013-04-28 09:12 288 查看
int 转 string

#include "stdafx.h"
#include<iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
//将int转成string
int n = 123456;
string s;

stringstream ss;
ss << n;
ss >> s;

cout<<s<<endl;

system("pause");
return 0;
}


string 转 int

#include "stdafx.h"
#include<iostream>
#include <string>
using namespace std;

int main()
{
//将string转成int
int n;
string s = "123456";

n = atoi(s.c_str());
cout<<n<<endl;

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: