atoi

What is the difference between std::atoi() and std::stoi?

有些话、适合烂在心里 提交于 2019-11-26 13:00:49
问题 What is the difference between atoi and stoi ? I know, std::string my_string = \"123456789\"; In order to convert that string to an integer, you’d have to do the following: const char* my_c_string = my_string.c_str(); int my_integer = atoi(my_c_string); C++11 offers a succinct replacement: std::string my_string = \"123456789\"; int my_integer = std::stoi(my_string); 1). Are there any other differences between the two? 2). Efficiency and performance wise which one is better? 3). Which is safer

Why do I get this unexpected result using atoi() in C?

女生的网名这么多〃 提交于 2019-11-26 09:58:55
问题 I don\'t understand the results of the following C code. main() { char s[] = \"AAA\"; advanceString(s); } void advanceString(p[3]) { int val = atoi(p); printf(\"The atoi val is %d\\n\",val); } Here the atoi value is shown as 0, but I could not figure out the exact reason. As per my understanding, it should be the summation of decimal equivalent of each values in the array? Please correct me if I am wrong. 回答1: atoi() converts a string representation of an integer into its value. It will not

How do I tell if the c function atoi failed or if it was a string of zeros?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 08:18:12
问题 When using the function atoi (or strtol or similar functions for that matter), how can you tell if the integer conversion failed or if the C-string that was being converted was a 0 ? For what I\'m doing, 0 is an acceptable value and the C-string being converted may contain any number of 0 s. It may also have leading whitespace. 回答1: For C++11 and later: The go-to function for string-to-integer conversion is now stoi, which takes a string and returns an int , or throws an exception on error.

atoi — how to identify the difference between zero and error?

末鹿安然 提交于 2019-11-26 04:27:34
问题 http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/ Return Value On success, the function returns the converted integral number as an int value. If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX or INT_MIN is returned. So how I differ between atoi(\"poop\") and atoi(\"0\") and atoi(\"0000000\") Yes I can loop and check for all zeroes in case I get 0 result, but isn\'t there a better way? Notice:

How to convert a string to integer in C?

眉间皱痕 提交于 2019-11-25 22:27:55
问题 I am trying to find out if there is an alternative way of converting string to integer in C. I regularly pattern the following in my code. char s[] = \"45\"; int num = atoi(s); So, is there a better way or another way? 回答1: There is strtol which is better IMO. Also I have taken a liking in strtonum, so use it if you have it (but remember it's not portable): long long strtonum(const char *nptr, long long minval, long long maxval, const char **errstr); EDIT You might also be interested in