简体   繁体   English

C++ - 负数和正数之间的比较返回 false

[英]C++ - Comparison between negative and positive number returns false

I am implementing a Queue using Vector in C++我在 C++ 中使用 Vector 实现队列

I use sz variable to keep track of how many free elements are in the queue at any insert/delete operation.我使用sz变量来跟踪在任何插入/删除操作中队列中有多少空闲元素。

I ran into an issue in my implementation where I was comparing:我在比较的实现中遇到了一个问题:

sz < vec.size() - 1

At some point, this line started to return 0 and the comparison was在某些时候,这条线开始返回0并且比较是

-1 < 5

One thing that helped was doing this有帮助的一件事是这样做

int vecsz = vec.size() - 1;
int regsz = sz;

if (regsz < vecsz)
{
    sz++;
}

Even though it helped, I still don't understand why.尽管它有所帮助,但我仍然不明白为什么。 Can someone shed some light on this please?有人可以对此有所了解吗?

queue.h队列.h

#include <iostream>
#include <vector>

using namespace std;

template <typename Object>
class queue
{
public:
    queue(int s) 
      : vec(s) 
    {
        sz = s - 1;
    }
    queue () { }
    ~queue() { }

    /**
     * Add element to Q and decrease size 
     */
    void enque(Object obj)
    {
        if (sz >= 0) {
            vec[sz] = obj;
            sz--;
        }
    }

    /**
     * Remove element from Q and increase size 
     */
    Object deque()
    {
        // grab last element
        int last = vec[vec.size() - 1];

        // remove last element from array
        vec.pop_back();

        // insert 0 in the front
        vec.insert(vec.begin(), 0);

        int vecsz = vec.size() - 1;
        int regsz = sz;

        // increase size tracking var
        if (regsz < vecsz)
        {
            sz++;
        }

        return last;
    }

    void print() {
        for (int i = 0; i < vec.size(); ++i) {
            cout << "i: " << vec[i] << endl;
        }
    }

private:
    vector<int> vec;
    int sz;
};

main主要的

int main()
{
    queue<int> Q(5);
    cout << "Empty Q:" << endl; 

    Q.print(); 
    cout << endl;

    Q.enque(1);
    Q.enque(2);
    Q.enque(3);

    cout << "Full Q1:" << endl; 

    Q.print(); 

    cout << "Deque " << endl;
    cout << Q.deque() << " " << Q.deque() << " " << Q.deque() << " " << endl;
    cout << Q.deque() << endl;

    cout << "Print " << endl;
    Q.print(); 

    cout << "Enqueue " << endl;
    Q.enque(10);
    Q.enque(20);
    Q.enque(30);
    Q.enque(40);
    Q.enque(50);

    cout << "\nFull Q2:" << endl; 
    Q.print(); 

    cout << endl;
    cout << Q.deque() << endl;

    Q.enque(100);

    cout << "Full Q3:"; Q.print(); cout << endl;

    cout << Q.deque() << endl;
    cout << Q.deque() << endl;
    cout << Q.deque() << endl;
    cout << Q.deque() << endl;

    return 0;
}

This is why it is important no to mix signed and unsigned types.这就是为什么不要混合有符号和无符号类型很重要的原因。 With

sz < vec.size() - 1

you are comparing an signed integer to an unsigned integer.您正在将已签名的 integer 与未签名的 integer 进行比较。 When you do that, the compiler converts the signed integer to the same type as the unsigned integer.当您这样做时,编译器会将带符号的 integer 转换为与无符号 integer 相同的类型。 When you do that and the signed integer happens to be negative, then you get a value that is max value of the unsigned type, minus the value of the signed integer So当你这样做并且有符号的 integer 恰好是负数时,你会得到一个值,它是无符号类型的最大值,减去有符号的 integer 的值所以

-1 < 5

becomes变成

4294967295 (for 32 bits) or 18446744073709551615 (for 64 bits) < 5

which is false.这是错误的。

To fix this, make sz an unsigned type.要解决此问题, sz无符号类型。

vec.size() is an unsigned integer. vec.size()是一个无符号 integer。 When you compare an unsigned integer type to a signed integer type of same size or smaller, the signed type will be converted to the same type as the other.当您将无符号 integer 类型与相同大小或更小的有符号 integer 类型进行比较时,有符号类型将转换为与另一个相同的类型。 Negative numbers are not representable by unsigned types.无符号类型不能表示负数。

In general, you should avoid comparing signed numbers with unsigned numbers, because of the problems that you've observed.通常,由于您观察到的问题,您应该避免将有符号数与无符号数进行比较。

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

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