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>
(首篇博文)
本文详细介绍了如何将字符串转换为整型数值的方法,包括使用sscanf、流操作和C标准库函数。通过实例代码演示了每种方法的实现过程。
1211

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



