C++ comparing bunch of values with a given one

后端 未结 10 1058
一向
一向 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 07:07

    I would recommend your method 2, using a std::vector or other container and looking for membership. Since the order of elements that you're checking against is probably not relevant, you may want to use a std::set or std::map. If you have very many items in your set of values, then using a set or a map will be a faster, while if you have only a few a vector may be faster.

    The advantage of any of these approaches is that you can then store the set/map somewhere common, and avoid having to build the set of matching responses every time.

提交回复
热议问题