0

There is an integer vector inorder to which I have pushed 5 elements. The variable (int) pointer has a value -1. Declared as:

    vector<int> inorder;
    int pointer;

When I execute the following statement, it gives a false.

   cout << (pointer < inorder.size()-1); 

However, when I change the statement to the following, it gives a true:

    cout<< (pointer+1 < inorder.size());

Why is this happening? The size of the vector is 5, so the problem of (0-1) leading to a very large value should not be there.

EDIT: adding the code to reproduce the issue:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
    vector<int> inorder;
    inorder.push_back(1);
    inorder.push_back(2);
    inorder.push_back(3);
    int pointer = -1;

    cout<<(pointer < inorder.size()-1)<<endl;
    cout<<(pointer+1 < inorder.size());
    return 0;
}
Ishani Vij
  • 138
  • 1
  • 12
  • 1
    `size()` returns a `std::vector::size_type` which is unsigned. – François Andrieux Dec 10 '20 at 19:35
  • could you please explain a little more? how does shifting `1` to the left side in comparison expression make any difference? – Ishani Vij Dec 10 '20 at 19:36
  • the size of vector is not 0. It is 5 – Ishani Vij Dec 10 '20 at 19:37
  • @FrançoisAndrieux this question is not a duplicate for that. I have a non-zero sized vector – Ishani Vij Dec 10 '20 at 19:40
  • 1
    Then it is a duplicate of [Why is a negative int greater than unsigned int?](https://stackoverflow.com/questions/13600991/why-is-a-negative-int-greater-than-unsigned-int). – François Andrieux Dec 10 '20 at 19:43
  • 1
    Works on my machine. You should provide a complete, and minimal code that reproduces the problem. [mcve] – Eljay Dec 10 '20 at 19:46
  • 1
    @Eljay But it doesn't work : https://godbolt.org/z/h4P5Kf The expected behavior is that both comparisons are equivalent. The question is why are they not. – François Andrieux Dec 10 '20 at 19:53
  • @IshaniVij I cannot close a question twice, but see my earlier comment for the correct duplicate. – François Andrieux Dec 10 '20 at 19:56
  • 1
    @IshaniVij Your compiler didn't warn you about comparing signed and unsigned types? – PaulMcKenzie Dec 10 '20 at 19:56
  • 1
    My cobbled together code from the (previously) sparse code given worked, because I had to provide some casts to get it to compile. The full example given has an `int` on one side, and (probably) an `unsigned long` on the other. The `int` gets converted to an `unsigned long`, which means the -1 becomes 18446744073709551615. 18446744073709551615 is bigger than (3-1). But 18446744073709551615+1 becomes 0 (which is a well defined behavior in the standard), and 0 is less than 3. – Eljay Dec 10 '20 at 20:04
  • @FrançoisAndrieux Thankyou. Understood the working. Without adding 1, the value of LHS becomes equal to UINT_MAX (UINT_MAX+1 + `-1`), but when 1 is shifted to the left side, the addition of 1 makes LHS equal to 0. – Ishani Vij Dec 10 '20 at 20:04
  • @PaulMcKenzie No, there was no warning. But now I understand how dangerous this can be! – Ishani Vij Dec 10 '20 at 20:06
  • 2
    **Recommended reading:** [Why should I not #include ?](https://stackoverflow.com/q/31816095/560648) – Asteroids With Wings Dec 10 '20 at 20:08
  • I have my compiler's warnings all (well, almost all) turned on, and have them treated as errors. The compiler helps me from hurting myself. – Eljay Dec 10 '20 at 20:08
  • 1
    @Eljay Wise. What isn't wise, though, is adding switches that the OP didn't say they're using, then modify the code to work around their effects, then report your observations about that _modified_ code to the OP as if it were their code (_"Works on my machine"_) – Asteroids With Wings Dec 10 '20 at 20:11
  • @AsteroidsWithWings • That is why I asked for a complete working example, instead of snippets. So I wouldn't have to guess if the problem was in the OP's code provided, or in the OP's code not provided. – Eljay Dec 10 '20 at 20:17

0 Answers0