C++ atoi 怎么用

2024-11-28 04:21:52
推荐回答(2个)
回答1:

string的成员substr返回的是string类型的对象,而atoi函数要求参数为char*型字符串,所以可以使用string的c_str成员函数,对程序作如下修改即可:

a[j] = atoi( achar.substr(j, 1).c_str() );

回答2:

a[j] = atoi(achar.substr(j, 1)).c_str();

const char* c_str ( ) const;

Get C string equivalent

Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.

A terminating null character is automatically appended.

The returned array points to an internal location with the required storage space for this sequence of characters plus its terminating null-character, but the values in this array should not be modified in the program and are only granted to remain unchanged until the next call to a non-constant member function of the string object.

Parameters
none

Return Value
Pointer to an internal array containing the c-string equivalent to the string content.