atoi和字符串数组

bah*_*rtr 0 c++ arrays string atoi

我有一个字符串数组和一个整数数组.我想将字符串数组的元素转换为整数,然后将它们存储在整数数组中.我写了这段代码:

string yuzy[360];
int yuza[360];

for(int x = 0;x<360;x++)
{
    if(yuzy[x].empty() == false)
    {

         yuza[x]=atoi(yuzy[x]);
         cout<<yuza[x]<<endl;
    }
    else
        continue;
}
Run Code Online (Sandbox Code Playgroud)

这段代码给出了这个错误:错误:无法将'std :: string {aka std :: basic_string}'转换为'const char*'以将参数'1'转换为'int atoi(con​​st char*)'

当我在atoi函数中写入字符串的内容(-75dbm)时,它工作正常.但是当我写(yuzy [x])时,我得到了错误.如何使atoi在字符串数组中运行良好?谢谢.

小智 8

atoi()接受C字符串(char指针)而不是C++字符串对象.使用

atoi(yuzy[x].c_str());
Run Code Online (Sandbox Code Playgroud)

代替.