当我string在下面的代码中传递一个变量时,g ++给出了一个错误:
不能将'std :: __ cxx11 :: string {aka std :: __ cxx11 :: basic_string}'转换为'const char*'以将参数'1'转换为'int atoi(const char*)'
我的代码是:
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
string a = "10";
int b = atoi(a);
cout<<b<<"\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将代码更改为:
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
char a[3] = "10";
int b = atoi(a);
cout<<b<<"\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它工作得很好.
请解释为什么string不起作用.有什么区别string a和char a[]?