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

    xiaoxiao2021-03-25  112

    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()函数,这个函数则需要加头文件

    转载请注明原文地址: https://ju.6miu.com/read-19888.html

    最新回复(0)