简体   繁体   English

如何获得std :: numeric_limits <char> :: min()值?

[英]How to get std::numeric_limits<char>::min() value?

How to get (correctly and/or readable value of) std::numeric_limits<char>::min() ? 如何获得(正确和/或可读的值) std::numeric_limits<char>::min()

cout << std::numeric_limits<char>::min() << endl;
cout << std::numeric_limits<char>::max() << endl;

return 返回

�
  // some character that can't be copied here, it looks like a rectangle containing four numbers in it                  

You just need to convert it to something that when streamed to cout will interpret it as an integer. 您只需要将其转换为流式传输到cout将其解释为整数的内容。 Eg 例如

#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;
}

I deliberately use unsigned for std::numeric_limits<char>::max() just in case sizeof(int) == 1 and char is unsigned. 我故意使用unsigned for std::numeric_limits<char>::max() ,以防sizeof(int) == 1char是无符号的。

The problem is that the standard streams will output char s as characters and not as integral values. 问题是标准流将char输出为字符而不是整数值。 You can force them to do that by casting to an integral type that isn't a character type: 您可以通过强制转换为非字符类型的整数类型来强制它们执行此操作:

cout << (int)std::numeric_limits<char>::min() << endl;
cout << (int)std::numeric_limits<char>::max() << endl;

Just cast it to an int . 只需将其转换为int

cout << (int)std::numeric_limits<char>::min() << endl;
cout << (int)std::numeric_limits<char>::max() << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM