C++ comparing bunch of values with a given one

后端 未结 10 930
一向
一向 2021-01-21 06:02

I need to compare one given value with a retrieved values. I do this several times in the code. I am not satisfied with how it looks and I am seeking for a some sort of an util

10条回答
  •  醉话见心
    2021-01-21 06:53

    If the values you're looking for are Comparable with operator< (like ints, float and std::strings), then it's faster to use an std::set to put the values there and then check set.find(value) == set.end(). This is because the set will store the values with a certain order that allows for faster lookups. Using an hash table will be even faster. However, for less than 50 values or so you might not notice any difference :) So my rule of thumb would be:

    • Less then 5 items: if with multiple ||

    • 5 or more: put in a set or hash table

提交回复
热议问题