c++ - int n != unsigned int(n) 的含义

标签 c++ file c++11 ppm

我正在阅读一个代码段,该代码段正在检查 ppm 文件中的某些值是否正确。这些值是这样读取的:

image >> img_format >> width >> height >> maxval;

然后检查宽度和高度是否正确,使用的是:

if (width != unsigned int(width) && height != unsigned int(height))
{
    cerr << "Width and/or height of the image are missing." << endl;
    exit(EXIT_FAILURE);
}

**widthheight 声明为 unsigned int

这个条件是如何工作的?

最佳答案

Unsigned int 表示数字为 0 或任何正数。在您的情况下,它正在检查变量 width 和 height 是否均为 0 或某个正整数。

旁注:Size_t 是另一种表示无符号整数的数据类型。

关于c++ - int n != unsigned int(n) 的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53556856/

相关文章:

c++ - 转换在路径和矩形中的工作方式不同

arrays - Python 3.73 插入字节数组 = "object cannot be re-sized"

c++ - 将共享指针与在另一个函数中分配的内存一起使用

c++ - 从非模板化接口(interface)检索 'generic'数据

c++ - 不能为我的数组大小使用变量,但变量被声明为 const int

c++ - 只读内存映射寄存器在 C 中定义为 `volatile const` 但在 C++ 中仅定义为 `volatile`

c++ - 谁能检查这个 Palindrome c++ 代码是否正确?

python - 路径内文件夹内文件内的深度小写

Java BufferedReader 回到文本文件的顶部?

c++ - 根据字符组合拆分字符串/在 `\n`