Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to compare a variable to multiple values?

A few times in my program, I've had to check if a variable was one of many options. For example

if (num = (<1 or 2 or 3>)) { DO STUFF } 

I've messed around with 'OR's, but nothing seems to be right. I've tried

if (num == (1 || 2 || 3)) 

but it does nothing.

I'd like to conveniently distinguish between several groups. For example

if (num = (1,2,3))  else if (num = (4,5,6))  else if (num = (7,8,9)) 
like image 487
Matt Reynolds Avatar asked Mar 03 '13 01:03

Matt Reynolds


People also ask

How do you compare variables with multiple values?

Use scatterplots to compare two continuous variables. Use scatterplot matrices to compare several pairs of continuous variables. Use side-by-side box plots to compare one continuous and one categorical variable. Use variability charts to compare one continuous Y variable to one or more categorical X variables.

What chart is suitable for comparing multiple values?

The column chart is probably the most used chart type. This chart is best used to compare different values when specific values are important, and it is expected that users will look up and compare individual values between each column.

How do you compare three values in an if statement?

To compare 3 values, use the logical AND (&&) operator to chain multiple conditions. When using the logical AND (&&) operator, all conditions have to return a truthy value for the if block to run. Copied! In the code example, we used the logical AND (&&) operator to chain two conditions.

What is used to compare two variables?

In order to measure the strength of a linear relationship between two quantitative variables we use correlation. Correlation is the measure of the strength of a linear relationship. We calculate correlation in Minitab by (using the Exam Data):


2 Answers

Here's a way in C++11, using std::initializer_list:

#include <algorithm> #include <initializer_list>  template <typename T> bool is_in(const T& v, std::initializer_list<T> lst) {     return std::find(std::begin(lst), std::end(lst), v) != std::end(lst); } 

with that, you can do:

if (is_in(num, {1, 2, 3})) { DO STUFF } 

It is not very efficient though when not used with built-in types. int will work fine, but if you compare std::string variables for example, the produced code is just awful.

In C++17 however, you can instead use a much more efficient solution that works well with any type:

template<typename First, typename ... T> bool is_in(First &&first, T && ... t) {     return ((first == t) || ...); }  // ...  // s1, s2, s3, s4 are strings. if (is_in(s1, s2, s3, s4)) // ... 

The C++11 version would be very inefficient here, while this version should produce the same code as hand-written comparisons.

like image 171
Nikos C. Avatar answered Oct 08 '22 04:10

Nikos C.


If the values you want to check are sufficiently small, you could create a bit mask of the values that you seek and then check for that bit to be set.

Suppose, you care about a couple of groups.

static const unsigned values_group_1 = (1 << 1) | (1 << 2) | (1 << 3); static const unsigned values_group_2 = (1 << 4) | (1 << 5) | (1 << 6); static const unsigned values_group_3 = (1 << 7) | (1 << 8) | (1 << 9);     if ((1 << value_to_check) & values_group_1) {   // You found a match for group 1 } if ((1 << value_to_check) & values_group_2) {   // You found a match for group 2 } if ((1 << value_to_check) & values_group_3) {   // You found a match for group 3 } 

This approach works best for values that don't exceed the natural size your CPU likes to work with. This would typically be 64 in modern times, but may vary depending upon the specifics of your environment.

like image 40
Eric Johnson Avatar answered Oct 08 '22 02:10

Eric Johnson