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

C\C++:string与int相互转换1

2014-09-03 19:31 441 查看
string转为int (x是int型)

1、用sscanf格式化字符串

string sn="1234";
sscanf(sn.c_str(),"%d",&x);
cout<<x<<endl;头文件#include<sstream>

2、使用流
string sn="1234"
istringstream i(sn);
if (i>>x)
cout<<x<<endl;头文件#include<stdio.h>

3、使用C lib
string sn="1234";
x=atoi(sn.c_str());头文件#include <stdlib.h>

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