Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to convert a string into an integer
How to convert a string into an integer
First of all, I would like to thank several Zhihu brothers for their advice. This article was published once on the topic of C++ atoi keyword. It's really my own fault. I didn't respect the teacher and didn't learn well. Thank you for your advice. Because of the need of work, the reason why I post articles on learning C++ in Zhihu is just to record my study notes, give myself some feedback on the results and promote my study. Some places need to be considered and even made mistakes so as not to mislead other people's children. Please give me more advice. Even the ridicule and contempt for me are acceptable, thank you very much!

function

The atoi () function converts a string in numeric format to an integer type. For example, the character string1253124127 is converted into the number1253124127.

pay attention to

Main attention

The Atoi function can be converted into the following string 1fdafhdjfhkas.

Matters needing attention about parameters

The parameter of the atoi () function is the string to be converted. The format of the string is

[space] [symbol] [number]

Among them, the space can be a space character in the keyboard or a tab; The symbol can be "+"for positive number or "-"for negative number; Numbers are strings of numbers. Therefore, the parameters of the atoi () function can be

+ 123

-456

It should be noted that spaces and "+"can be omitted. Therefore, the parameter of the atoi () function can also be

123

-456

Matters needing attention about return value

If the atoi () function is successfully converted, the return value of the function is the converted integer. If the conversion of atoi () function fails, for example, the type to be converted is beyond the range indicated by int, if the conversion is positive, it will return INT_MAX(2 147483647), if the conversion is negative, it will return INT_MIN(-2 147483648). The code is as follows

Calling method

//The format of this function is

Int atoi (constant char* str)

//where the parameter str is the string to be converted and the return value is the converted integer.

int main(){

const char * str 1 = " 124932657 13256 "

const char* str2="8fdafhdjfhkas "

int outNm=atoi(str 1)

}