开发者

Can `std::istream::operator>>()` accept integer radix prefixes like stdio's %i format specifier?

When using scanf() and its variants, the format specifier %i will accept data as hex (prefixed "0x"), octal (prefixed "0"), or decimal (no prefix), so for example the strings "0x10", "020", and "16" are all converted to an integer with decimal value 16.

Can this be done with std::istream::operator>> formatted input?

Using plain >> i with no i/o manipulator "0x10" is converted to zero (or rath开发者_Python百科er the leading 0 is, the "x10" part is not processed), and "020" to 20. The hex, oct and dec manipulators behave like %x, %o and %d respectively. I am looking for a general integer input manipulator that works like %i.

Interestingly perhaps the hex manipulator accepts both "0x10" and "10" converting either to 16 decimal.

In case you might be wondering, I am implementing an expression evaluator, and I would like allowed integer operands to be hex, octal, or decimal using the C/C++ prefix convention. The current implementation using sscanf() does this automatically using %i, and I am curious as to whether this could be modified to use iostream without having to explicitly parse the numeric format.


The base field in the format flags of basic_istream dictates how to interpret numbers. The field can be set to dec, oct and to hex. By default it's set to dec. If it's set to none of them, basic_istream will behave like scanf's %i flag:

// automatically detect the base, depending on prefix
std::cin.unsetf(std::ios_base::basefield);


Instead of resetting the basefield directly, a slightly more intuitive solution is to use the setbase(int base) manipulator:

#include <iomanip>
#include <iostream>

int main()
{
  int i;
  std::cin >> std::setbase(0) >> i;
  std::cout << i << std::endl;
}

To quote cppreference:

Values of base other than 8, 10, or 16 reset basefield to zero, which corresponds to decimal output and prefix-dependent input.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜