Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert string to Integer in C++

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how C++ can convert strings to integers. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

String to Integer (atoi) string to integer

Implement atoi which converts a string to an integer.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

Note:

Only the space character "" is considered as whitespace character.

Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [− 231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (− 231) is returned.

Example 1:

Input: 42

Output: 42

Example 2:

Input: "- 42"

Output:-42

Explanation: The first non-whitespace character is "-", which is the minus sign.

Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"

Output: 4193

Explanation: Conversion stops at digit "3" as the next character is not a numerical digit.

Example 4:

Input: "words and 987"

Output: 0

Explanation: The first non-whitespace character is "w", which is not a numerical

Digit or a + /-sign. Therefore no valid conversion could be performed.

Example 5:

Input: "- 91283472332"

Output:-2147483648

Explanation: The number "- 91283472332" is out of the range of a 32-bit signed integer.

Thefore INT_MIN (− 231) is returned.

String to integer is a very common function, because the input is a string, so there are many situations to consider. The blogger had an article about verifying whether a string is a number, see Valid Number. In that article, various situations are discussed in detail, including symbols, natural numbers, and the location of the decimal point to determine whether they are numbers or not. Personally, I think there should be so many situations in this problem. But this question only needs to consider the situation of numbers and symbols:

1. If the string begins with a space, all spaces are skipped to the first non-space character, and if not, 0.

two。 If the first non-white space character is the symbol + / -, then mark the true or false of sign. This question also has a limitation, that is, in C++, +-1 and-+ 1 are both recognized, both are-1, while in this question, 0 is returned.

3. If the next character is not a number, 0 is returned, regardless of decimal point and natural number, but this is good, at least it saves a lot of trouble.

4. If the next character is a number, it is converted to shaping and saved, and if a non-number appears next, the current result is returned.

5. You also need to consider the boundary problem, and if you exceed the range of integers, replace the current value with the boundary value.

C++ solution:

Class Solution {public: int myAtoi (string str) {if (str.empty ()) return 0; int sign = 1, base = 0, I = 0, n = str.size (); while (I

< n && str[i] == " ") ++i; if (i < n && (str[i] == "+" || str[i] == "-")) { sign = (str[i++] == "+") ? 1 : -1; } while (i < n && str[i] >

= "0" & & str [I] INT_MAX / 10 | (base = = INT_MAX / 10 & & str [I]-"0" > 7) {return (sign = = 1)? INT_MAX: INT_MIN;} base = 10 * base + (str [iTunes +]-"0");} return base * sign;}}

Java solution:

Public class Solution {public int myAtoi (String str) {if (str.isEmpty ()) return 0; int sign = 1, base = 0, I = 0, n = str.length (); while (I

< n && str.charAt(i) == " ") ++i; if (i < n && (str.charAt(i) == "+" || str.charAt(i) == "-")) { sign = (str.charAt(i++) == "+") ? 1 : -1; } while (i < n && str.charAt(i) >

= "0" & & str.charAt (I) Integer.MAX_VALUE / 10 | (base = = Integer.MAX_VALUE / 10 & & str.charAt (I)-"0" > 7) {return (sign = = 1)? Integer.MAX_VALUE: Integer.MIN_VALUE;} base = 10 * base + (str.charAt (iTunes +)-"0");} return base * sign;}} above is all the content of the article "how to convert strings to integers in C++". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report