c++基础:如何string类型转换为int类型

来源:互联网 发布:有试衣间的淘宝店铺 编辑:程序博客网 时间:2024/06/03 16:47

c++中如何把string类型转换int类型,常用的方法就是利用atoi函数,使用这个函数需要加头文件

#include <iostream> #include <stdlib.h>using namespace std;  int main ()  {      int num;      char arr [10];      cout<<"Enter a number: "<<endl;      cin >> arr;     num = atoi (arr);      cout<<"The value entered is :"<<arr<<endl;      cout<<" The number convert is:"<<num<<endl;     return 0;  }  

INPUT:100e
OUTPUT:
The value entered is :100e
The number convert is:100

其他:如果是传递给了string&类型,想要转换成int,需要加c_str()函数,这个函数则需要加头文件

0 0
原创粉丝点击