C++ strtol的基础知识?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14794079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:42:01  来源:igfitidea点击:

Basics of strtol?

c++stringintstrtol

提问by James Thompson

I am really confused. I have to be missing something rather simple but nothing I am reading about strtol() is making sense. Can someone spell it out for me in a really basic way, as well as give an example for how I might get something like the following to work?

我真的很困惑。我必须遗漏一些相当简单的东西,但我读到的关于 strtol() 的内容没有任何意义。有人可以以非常基本的方式为我拼写出来,并举例说明我如何使以下内容起作用?

string input = getUserInput;
int numberinput = strtol(input,?,?);

回答by nneonneo

The first argument is the string. It has to be passed in as a C string, so if you have a std::stringuse .c_str()first.

第一个参数是字符串。它必须作为 C 字符串传入,所以如果您先std::string使用它.c_str()

The second argument is optional, and specifies a char *to store a pointer to the character after the end of the number. This is useful when converting a string containing several integers, but if you don't need it, just set this argument to NULL.

第二个参数是可选的,它指定 achar *来存储指向数字结尾后的字符的指针。这在转换包含多个整数的字符串时很有用,但如果您不需要它,只需将此参数设置为 NULL。

The third argument is the radix (base) to convert. strtolcan do anything from binary (base 2) to base 36. If you want strtolto pick the base automatically based on prefix, pass in 0.

第三个参数是要转换的基数(基数)。strtol可以执行从二进制(基数 2)到基数 36 的任何操作。如果您想strtol根据前缀自动选择基数,请传入 0。

So, the simplest usage would be

所以,最简单的用法是

long l = strtol(input.c_str(), NULL, 0);

If you know you are getting decimal numbers:

如果你知道你得到的是十进制数:

long l = strtol(input.c_str(), NULL, 10);

strtolreturns 0 if there are no convertible characters at the start of the string. If you want to check if strtolsucceeded, use the middle argument:

strtol如果字符串开头没有可转换的字符,则返回 0。如果要检查是否strtol成功,请使用中间参数:

const char *s = input.c_str();
char *t;
long l = strtol(s, &t, 10);
if(s == t) {
    /* strtol failed */
}


If you're using C++11, use stolinstead:

如果您使用的是 C++11,请stol改用:

long l = stol(input);

Alternately, you can just use a stringstream, which has the advantage of being able to read many items with ease just like cin:

或者,您可以只使用 a stringstream,它的优点是能够轻松读取许多项目,例如cin

stringstream ss(input);
long l;
ss >> l;

回答by Kerrek SB

Suppose you're given a string char const * str. Now convert it like this:

假设你得到一个 string char const * str。现在像这样转换它:

#include <cstdlib>
#include <cerrno>

char * e;
errno = 0;

long n = std::strtol(str, &e, 0);

The last argument 0determines the number base you want to apply; 0means "auto-detect". Other sensible values are 8, 10or 16.

最后一个参数0决定了你想要应用的数基;0表示“自动检测”。其他合理的值是8,1016

Next you need to inspect the end pointer e. This points to the character after the consumed input. Thus if all input was consumed, it points to the null-terminator.

接下来您需要检查结束指针e。这指向消费输入之后的字符。因此,如果所有输入都被消耗了,则它指向空终止符。

if (*e != '
if (errno != 0) { /* error, die */ }
') { /* error, die */ }

It's also possible to allow for partial input consumption using e, but that's the sort of stuff that you'll understand when you actually need it.

也可以使用 允许部分输入消耗e,但这是您在实际需要时会理解的那种东西。

Lastly, you should check for errors, which can essentially only be overflow errors if the input doesn't fit into the destination type:

最后,您应该检查错误,如果输入不适合目标类型,则基本上只能是溢出错误:

#include <string>

try { long n = std::stol(str); }
catch (std::invalid_argument const & e) { /* error */ }
catch (std::out_of_range const & e)     { /* error */ }


In C++, it might be preferable to use std::stol, though you don't get to pick the number base in this case:

在 C++ 中,使用 可能更可取std::stol,尽管在这种情况下您不能选择数字基数:

long int strtol ( const char * str, char ** endptr, int base );

回答by Patashu

Quote from C++ reference:

引用C++ 参考

long l = strtol(pointerToStartOfString, NULL, 0)

Convert string to long integer

将字符串转换为长整数

Parses the C string str interpreting its content as an integral number of the specified base, which is returned as a long int value. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.

解析 C 字符串 str ,将其内容解释为指定基数的整数,并作为 long int 值返回。如果 endptr 不是空指针,该函数还会将 endptr 的值设置为指向数字后的第一个字符。

So try something like

所以尝试类似的东西

##代码##

回答by mvp

I always use simply strol(str,0,0)- it returns long value. 0 for radix (last parameter) means to auto-detect it from input string, so both 0x10as hex and 10as decimal could be used in input string.

我总是简单地使用strol(str,0,0)- 它返回长值。0 表示基数(最后一个参数)表示从输入字符串中自动检测它,因此在输入字符串中可以使用0x10十六进制和10十进制。