Comparison tricks in C++

前端 未结 10 1870
礼貌的吻别
礼貌的吻别 2021-01-31 16:13

A class:

class foo{
public:
    int data;
};

Now I want to add a method to this class, to do some comparison, to see if its data is equal to on

10条回答
  •  梦谈多话
    2021-01-31 16:49

    Any optimization is going to depend on properties of the set of numbers being compared to.

    If there's a definite upper bound, you can use a std::bitset. Testing membership (that is, indexing into the bitset, which behaves like an array), is O(1), effectively a few fast instructions. This is often the best solution up to limits in the hundreds, although depending on the application millions could be practical.

提交回复
热议问题