数据的接收与转换
- 将string型的数字转化为int型的数字
#include <iostream> #include <string> using namespace std; int main() { string str; cin>>str; int b = str[0]-'0'; }
- 从数字与符号混合的string中分离出int类型的数字
#include <iostream> #include <string> using namespace std; int main() { string str; cin>>str; for(int i=0;i<str.size();i++) { int j=i,x=0; if(isdigit(str[i])) { while(isdigit(str[j]) && j<str.size()) { x=x*10+str[j++]-'0'; } i=j-1; cout<<x;//每一个x都是一个int类型的数字 } } }
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...