如何获得(正确和/或可读的价值)std::numeric_limits<char>::min()?
cout << std::numeric_limits<char>::min() << endl;
cout << std::numeric_limits<char>::max() << endl;
Run Code Online (Sandbox Code Playgroud)
返回
?
// some character that can't be copied here, it looks like a rectangle containing four numbers in it
Run Code Online (Sandbox Code Playgroud)
您只需要将其转换为流式传输时cout将其解释为整数的内容.例如
#include <limits>
#include <iostream>
#include <ostream>
int main()
{
int minc = std::numeric_limits<char>::min();
unsigned maxc = std::numeric_limits<char>::max();
std::cout << minc << std::endl;
std::cout << maxc << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我特意使用unsigned了std::numeric_limits<char>::max()的情况下,只是sizeof(int) == 1和char无符号.
问题是标准流将输出chars作为字符而不是整数值.您可以通过强制转换为非字符类型的整数类型来强制它们执行此操作:
cout << (int)std::numeric_limits<char>::min() << endl;
cout << (int)std::numeric_limits<char>::max() << endl;
Run Code Online (Sandbox Code Playgroud)