自己大致实现的,有点粗糙
int Atoi(const string& str)
{
int ret = 0;
bool bFirst = false;
bool bMinus = false;
for(int i=0; i < str.length(); ++i)
{
if(str[i] == ' ') continue;
if(str[i] == '-' && bFirst == false)
{
bMinus = true;
continue;
}
if(str[i] >= '0' && str[i] <= '9')
{
if(bFirst == false) bFirst = true;
ret = ret*10+str[i]-'0';
}
else if(str[i] != ' ')
{
break;
}
}
if(bMinus) ret = -ret;
return ret;
}
int main()
{
string str = " -1234a";
int ret = Atoi(str);
printf("result = %d \n", ret);
}