• <small id='D1lXA'></small><noframes id='D1lXA'>

    <i id='D1lXA'><tr id='D1lXA'><dt id='D1lXA'><q id='D1lXA'><span id='D1lXA'><b id='D1lXA'><form id='D1lXA'><ins id='D1lXA'></ins><ul id='D1lXA'></ul><sub id='D1lXA'></sub></form><legend id='D1lXA'></legend><bdo id='D1lXA'><pre id='D1lXA'><center id='D1lXA'></center></pre></bdo></b><th id='D1lXA'></th></span></q></dt></tr></i><div id='D1lXA'><tfoot id='D1lXA'></tfoot><dl id='D1lXA'><fieldset id='D1lXA'></fieldset></dl></div>
  • <legend id='D1lXA'><style id='D1lXA'><dir id='D1lXA'><q id='D1lXA'></q></dir></style></legend>
    <tfoot id='D1lXA'></tfoot>

      <bdo id='D1lXA'></bdo><ul id='D1lXA'></ul>

        c ++ 矢量大小.为什么 -1 大于零

        时间:2023-09-16
          <tbody id='SROV0'></tbody>

          <bdo id='SROV0'></bdo><ul id='SROV0'></ul>
          <i id='SROV0'><tr id='SROV0'><dt id='SROV0'><q id='SROV0'><span id='SROV0'><b id='SROV0'><form id='SROV0'><ins id='SROV0'></ins><ul id='SROV0'></ul><sub id='SROV0'></sub></form><legend id='SROV0'></legend><bdo id='SROV0'><pre id='SROV0'><center id='SROV0'></center></pre></bdo></b><th id='SROV0'></th></span></q></dt></tr></i><div id='SROV0'><tfoot id='SROV0'></tfoot><dl id='SROV0'><fieldset id='SROV0'></fieldset></dl></div>
          <tfoot id='SROV0'></tfoot>

              <small id='SROV0'></small><noframes id='SROV0'>

            1. <legend id='SROV0'><style id='SROV0'><dir id='SROV0'><q id='SROV0'></q></dir></style></legend>

                • 本文介绍了c ++ 矢量大小.为什么 -1 大于零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  请看一下这个简单的程序:

                  Please take a look at this simple program:

                  #include <iostream>
                  #include <vector>
                  
                  using namespace std;
                  
                  int main() {
                  
                  vector<int> a;
                  
                  std::cout << "vector size " << a.size() << std::endl;
                  
                  int b = -1;
                  
                  if (b < a.size())
                     std::cout << "Less";
                  else
                     std::cout << "Greater";
                  
                      return 0;
                  }
                  

                  尽管 -1 明显小于 0,但它输出更大"这一事实让我感到困惑.我知道 size 方法返回无符号值,但比较仍然适用于 -1和 0. 那么发生了什么?谁能解释一下?

                  I'm confused by the fact that it outputs "Greater" despite it's obvious that -1 is less than 0. I understand that size method returns unsigned value but comparison is still applied to -1 and 0. So what's going on? can anyone explain this?

                  推荐答案

                  因为向量的大小是无符号整数类型.您正在将无符号类型与有符号类型进行比较,并且将二进制补码负有符号整数提升为无符号类型.这对应于一个大的无符号值.

                  Because the size of a vector is an unsigned integral type. You are comparing an unsigned type with a signed one, and the two's complement negative signed integer is being promoted to unsigned. That corresponds to a large unsigned value.

                  此代码示例显示了您所看到的相同行为:

                  This code sample shows the same behaviour that you are seeing:

                  #include <iostream>
                  int main()
                  {
                    std::cout << std::boolalpha;
                    unsigned int a = 0;
                    int b = -1;
                    std::cout << (b < a) << "
                  "; 
                  }
                  

                  输出:

                  这篇关于c ++ 矢量大小.为什么 -1 大于零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将文件读入 std::vector<char> 的有效方法? 下一篇:C++ 按值而不是按位置擦除向量元素?

                  相关文章

                  最新文章

                    <bdo id='Bmytz'></bdo><ul id='Bmytz'></ul>
                  <legend id='Bmytz'><style id='Bmytz'><dir id='Bmytz'><q id='Bmytz'></q></dir></style></legend>

                  1. <tfoot id='Bmytz'></tfoot>

                  2. <i id='Bmytz'><tr id='Bmytz'><dt id='Bmytz'><q id='Bmytz'><span id='Bmytz'><b id='Bmytz'><form id='Bmytz'><ins id='Bmytz'></ins><ul id='Bmytz'></ul><sub id='Bmytz'></sub></form><legend id='Bmytz'></legend><bdo id='Bmytz'><pre id='Bmytz'><center id='Bmytz'></center></pre></bdo></b><th id='Bmytz'></th></span></q></dt></tr></i><div id='Bmytz'><tfoot id='Bmytz'></tfoot><dl id='Bmytz'><fieldset id='Bmytz'></fieldset></dl></div>
                  3. <small id='Bmytz'></small><noframes id='Bmytz'>