📜  比较变量和多个值的有效方法

📅  最后修改于: 2021-05-30 17:44:22             🧑  作者: Mango

在本文中,我们将讨论将变量与值进行比较的方法。

方法1:想法是一次将每个变量与所有多个值进行比较。

程序1:

C++
// C++ program to compare one variable
// with multiple variable
#include 
using namespace std;
 
// Driver Code
int main()
{
    // Variable to be compared
    char character = 'a';
 
    // Compare the given variable
    // with vowel using || operator
 
    // Check for vowel
    if  (character == ('a' || 'e'
            || 'i' || 'o'
                    || 'u')) {
        cout << "Vowel";
    }
 
    // Otherwise Consonant
    else {
        cout << "Consonant";
    }
 
    return 0;
}


Java
// Java program to compare one variable
// with multiple variable
import java.util.*;
 
class GFG
{
 
// Driver Code
public static void main(String[] args)
{
    // Variable to be compared
    char character = 'a';
 
    // Compare the given variable
    // with vowel using || operator
    // Check for vowel
    if (character == ('a' || 'e'
            || 'i' || 'o'
                    || 'u')) {
        System.out.print("Vowel");
    }
 
    // Otherwise Consonant
    else {
        System.out.print("Consonant");
    }
 
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python program to compare one variable
# with multiple variable
 
# Driver Code
if __name__ == '__main__':
   
    # Variable to be compared
    character = 'a';
 
    # Compare the given variable
    # with vowel using or operator
    # Check for vowel
    if (character == ('a' or 'e' or 'i' or 'o' or 'u')):
        print("Vowel");
 
    # Otherwise Consonant
    else:
        print("Consonant");
 
# This code contributed by shikhasingrajput


C#
// C# program to compare one variable
// with multiple variable
using System;
 
class GFG
{
 
// Driver Code
public static void Main(String[] args)
{
    // Variable to be compared
    char character = 'a';
 
    // Compare the given variable
    // with vowel using || operator
    // Check for vowel
    if (character == ('a' || 'e'
            || 'i' || 'o'
                    || 'u')) {
        Console.Write("Vowel");
    }
 
    // Otherwise Consonant
    else {
        Console.Write("Consonant");
    }
}
}
 
// This code is contributed by 29AjayKumar


C++
// C++ program to compare one variable
// with multiple variable
#include 
using namespace std;
 
// Driver Code
int main()
{
    // Variable to be compared
    char character = 'a';
 
    // Compare the given variable
    // with vowel individually
 
    // Check for vowel
    if (character == 'a'
        || character == 'e'
        || character == 'i'
        || character == 'o'
        || character == 'u') {
        cout << "Vowel";
    }
 
    // Otherwise Consonant
    else {
        cout << "Consonant";
    }
 
    return 0;
}


Java
// Java program to compare
// one variable with multiple
// variable
import java.util.*;
class GFG{
 
// Driver Code
public static void main(String[] args)
{
  // Variable to be compared
  char character = 'a';
 
  // Compare the given variable
  // with vowel using || operator
 
  // Check for vowel
  if (character == 'a' || character == 'e' ||
      character == 'i' || character == 'o' ||
      character == 'u')
  {
    System.out.print("Vowel");
  }
 
  // Otherwise Consonant
  else
  {
    System.out.print("Consonant");
  }
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to compare
# one variable with multiple
# variable
 
# Driver Code
if __name__ == '__main__':
   
    # Variable to be compared
    character = 'a';
 
    # Compare the given variable
    # with vowel using or operator
 
    # Check for vowel
    if (character == 'a' or
        character == 'e' or
        character == 'i' or
        character == 'o' or
        character == 'u'):
        print("Vowel");
 
    # Otherwise Consonant
    else:
        print("Consonant");
 
# This code contributed by Princi Singh


C#
// C# program to compare
// one variable with multiple
// variable
using System;
class GFG{
 
// Driver Code
public static void Main(String[] args)
{
  // Variable to be compared
  char character = 'a';
 
  // Compare the given variable
  // with vowel using || operator
 
  // Check for vowel
  if (character == 'a' || character == 'e' ||
      character == 'i' || character == 'o' ||
      character == 'u')
  {
    Console.Write("Vowel");
  }
 
  // Otherwise Consonant
  else
  {
    Console.Write("Consonant");
  }
}
}
 
// This code is contributed by gauravrajput1


C++
// C++ program to compare a value
// with multiple values
#include 
using namespace std;
 
// Driver Code
int main()
{
    // Create bitmasks
    unsigned group_1 = (1 << 1) | (1 << 2) | (1 << 3);
    unsigned group_2 = (1 << 4) | (1 << 5) | (1 << 6);
    unsigned group_3 = (1 << 7) | (1 << 8) | (1 << 9);
 
    // Values to be checked
    int value_to_check = 9;
 
    // Checking with created bitmask
    if ((1 << value_to_check)
        & group_1) {
        cout << "found a match in group 1";
    }
    if ((1 << value_to_check)
        & group_2) {
        cout << "found a match in group 2";
    }
    if ((1 << value_to_check)
        & group_3) {
        cout << "found a match in group 3";
    }
 
    return 0;
}


Java
// Java program to compare a value
// with multiple values
import java.util.*;
class GFG{
 
// Driver Code
public static void main(String[] args)
{
    // Create bitmasks
    int group_1 = (1 << 1) |
                  (1 << 2) | (1 << 3);
    int group_2 = (1 << 4) |
                  (1 << 5) | (1 << 6);
    int group_3 = (1 << 7) |
                  (1 << 8) | (1 << 9);
 
    // Values to be checked
    int value_to_check = 9;
 
    // Checking with created bitmask
    if (((1 << value_to_check) &
          group_1) > 0)
    {
        System.out.print("found a match " +
                         "in group 1");
    }
    if (((1 << value_to_check) &
          group_2) > 0)
    {
        System.out.print("found a match " +
                         "in group 2");
    }
    if (((1 << value_to_check) &
          group_3) > 0)
    {
        System.out.print("found a match " +
                         "in group 3");
    }
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program to compare a value
# with multiple values
 
# Driver Code
if __name__ == '__main__':
     
    # Create bitmasks
    group_1 = (1 << 1) | (1 << 2) | (1 << 3)
    group_2 = (1 << 4) | (1 << 5) | (1 << 6)
    group_3 = (1 << 7) | (1 << 8) | (1 << 9)
     
    # Values to be checked
    value_to_check = 9
     
    # Checking with created bitmask
    if (((1 << value_to_check) & group_1) > 0):
        print("found a match " + "in group 1")
 
    if (((1 << value_to_check) & group_2) > 0):
        print("found a match " + "in group 2")
 
    if (((1 << value_to_check) & group_3) > 0):
        print("found a match " + "in group 3")
 
# This code is contributed by gauravrajput1


C#
// C# program to compare a value
// with multiple values
using System;
class GFG{
 
// Driver Code
public static void Main(String[] args)
{
  // Create bitmasks
  int group_1 = (1 << 1) |
                (1 << 2) |
                (1 << 3);
  int group_2 = (1 << 4) |
                (1 << 5) |
                (1 << 6);
  int group_3 = (1 << 7) |
                (1 << 8) |
                (1 << 9);
 
  // Values to be checked
  int value_to_check = 9;
 
  // Checking with created
  // bitmask
  if (((1 << value_to_check) &
        group_1) > 0)
  {
    Console.Write("found a match " +
                  "in group 1");
  }
  if (((1 << value_to_check) &
        group_2) > 0)
  {
    Console.Write("found a match " +
                  "in group 2");
  }
  if (((1 << value_to_check) &
        group_3) > 0)
  {
    Console.Write("found a match " +
                  "in group 3");
  }
}
}
 
// This code is contributed by gauravrajput1


C++
// C++ program for comparing variable
// with multiples variable
#include 
#include 
#include 
using namespace std;
 
template 
 
// Function that checks given variable
// v in the list lst[]
bool is_in(const T& v,
           std::initializer_list lst)
{
    return (std::find(std::begin(lst),
                      std::end(lst), v)
            != std::end(lst));
}
 
// Driver Code
int main()
{
    // Number to be compared
    int num = 10;
 
    // Compare with multiple variables
    if (is_in(num, { 1, 2, 3 }))
        cout << "Found in group" << endl;
    else
        cout << "Not in group" << endl;
 
    // Character to be compared
    char c = 'a';
 
    // Compare with multiple variables
    if (is_in(c, { 'x', 'a', 'c' }))
        cout << "Found in group" << endl;
    else
        cout << "Not in group" << endl;
 
    return 0;
}


C++
// C++ program for comparing variable
// with multiple variables
#include 
#include 
#include 
#include 
using namespace std;
 
template 
 
// Function that checks given variable
// first in the list t[]
bool is_in(First&& first, T&&... t)
{
    return ((first == t) || ...);
}
 
// Driver Code
int main()
{
    // Number to be compared
    int num = 10;
 
    // Compare using template
    if (is_in(num, 1, 2, 3))
        cout << "Found in group" << endl;
    else
        cout << "Not in group" << endl;
 
    // String to be compared
    string c = "abc";
 
    // Compare using template
    if (is_in(c, "xyz", "bhy", "abc"))
        cout << "Found in group" << endl;
    else
        cout << "Not in group" << endl;
 
    return 0;
}


输出
Consonant

解释:
上面的代码给出了错误的答案或错误,因为上面的方式中的比较变量是不正确的,并且它被强制按照下面的方式进行编码:

C++

// C++ program to compare one variable
// with multiple variable
#include 
using namespace std;
 
// Driver Code
int main()
{
    // Variable to be compared
    char character = 'a';
 
    // Compare the given variable
    // with vowel individually
 
    // Check for vowel
    if (character == 'a'
        || character == 'e'
        || character == 'i'
        || character == 'o'
        || character == 'u') {
        cout << "Vowel";
    }
 
    // Otherwise Consonant
    else {
        cout << "Consonant";
    }
 
    return 0;
}

Java

// Java program to compare
// one variable with multiple
// variable
import java.util.*;
class GFG{
 
// Driver Code
public static void main(String[] args)
{
  // Variable to be compared
  char character = 'a';
 
  // Compare the given variable
  // with vowel using || operator
 
  // Check for vowel
  if (character == 'a' || character == 'e' ||
      character == 'i' || character == 'o' ||
      character == 'u')
  {
    System.out.print("Vowel");
  }
 
  // Otherwise Consonant
  else
  {
    System.out.print("Consonant");
  }
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 program to compare
# one variable with multiple
# variable
 
# Driver Code
if __name__ == '__main__':
   
    # Variable to be compared
    character = 'a';
 
    # Compare the given variable
    # with vowel using or operator
 
    # Check for vowel
    if (character == 'a' or
        character == 'e' or
        character == 'i' or
        character == 'o' or
        character == 'u'):
        print("Vowel");
 
    # Otherwise Consonant
    else:
        print("Consonant");
 
# This code contributed by Princi Singh

C#

// C# program to compare
// one variable with multiple
// variable
using System;
class GFG{
 
// Driver Code
public static void Main(String[] args)
{
  // Variable to be compared
  char character = 'a';
 
  // Compare the given variable
  // with vowel using || operator
 
  // Check for vowel
  if (character == 'a' || character == 'e' ||
      character == 'i' || character == 'o' ||
      character == 'u')
  {
    Console.Write("Vowel");
  }
 
  // Otherwise Consonant
  else
  {
    Console.Write("Consonant");
  }
}
}
 
// This code is contributed by gauravrajput1
输出
Vowel

方法2 –使用位屏蔽:另一方法是在多组值之间进行检查,然后创建值的位屏蔽,然后检查要设置的位。

程式2:

C++

// C++ program to compare a value
// with multiple values
#include 
using namespace std;
 
// Driver Code
int main()
{
    // Create bitmasks
    unsigned group_1 = (1 << 1) | (1 << 2) | (1 << 3);
    unsigned group_2 = (1 << 4) | (1 << 5) | (1 << 6);
    unsigned group_3 = (1 << 7) | (1 << 8) | (1 << 9);
 
    // Values to be checked
    int value_to_check = 9;
 
    // Checking with created bitmask
    if ((1 << value_to_check)
        & group_1) {
        cout << "found a match in group 1";
    }
    if ((1 << value_to_check)
        & group_2) {
        cout << "found a match in group 2";
    }
    if ((1 << value_to_check)
        & group_3) {
        cout << "found a match in group 3";
    }
 
    return 0;
}

Java

// Java program to compare a value
// with multiple values
import java.util.*;
class GFG{
 
// Driver Code
public static void main(String[] args)
{
    // Create bitmasks
    int group_1 = (1 << 1) |
                  (1 << 2) | (1 << 3);
    int group_2 = (1 << 4) |
                  (1 << 5) | (1 << 6);
    int group_3 = (1 << 7) |
                  (1 << 8) | (1 << 9);
 
    // Values to be checked
    int value_to_check = 9;
 
    // Checking with created bitmask
    if (((1 << value_to_check) &
          group_1) > 0)
    {
        System.out.print("found a match " +
                         "in group 1");
    }
    if (((1 << value_to_check) &
          group_2) > 0)
    {
        System.out.print("found a match " +
                         "in group 2");
    }
    if (((1 << value_to_check) &
          group_3) > 0)
    {
        System.out.print("found a match " +
                         "in group 3");
    }
}
}
 
// This code is contributed by shikhasingrajput

Python3

# Python3 program to compare a value
# with multiple values
 
# Driver Code
if __name__ == '__main__':
     
    # Create bitmasks
    group_1 = (1 << 1) | (1 << 2) | (1 << 3)
    group_2 = (1 << 4) | (1 << 5) | (1 << 6)
    group_3 = (1 << 7) | (1 << 8) | (1 << 9)
     
    # Values to be checked
    value_to_check = 9
     
    # Checking with created bitmask
    if (((1 << value_to_check) & group_1) > 0):
        print("found a match " + "in group 1")
 
    if (((1 << value_to_check) & group_2) > 0):
        print("found a match " + "in group 2")
 
    if (((1 << value_to_check) & group_3) > 0):
        print("found a match " + "in group 3")
 
# This code is contributed by gauravrajput1

C#

// C# program to compare a value
// with multiple values
using System;
class GFG{
 
// Driver Code
public static void Main(String[] args)
{
  // Create bitmasks
  int group_1 = (1 << 1) |
                (1 << 2) |
                (1 << 3);
  int group_2 = (1 << 4) |
                (1 << 5) |
                (1 << 6);
  int group_3 = (1 << 7) |
                (1 << 8) |
                (1 << 9);
 
  // Values to be checked
  int value_to_check = 9;
 
  // Checking with created
  // bitmask
  if (((1 << value_to_check) &
        group_1) > 0)
  {
    Console.Write("found a match " +
                  "in group 1");
  }
  if (((1 << value_to_check) &
        group_2) > 0)
  {
    Console.Write("found a match " +
                  "in group 2");
  }
  if (((1 << value_to_check) &
        group_3) > 0)
  {
    Console.Write("found a match " +
                  "in group 3");
  }
}
}
 
// This code is contributed by gauravrajput1
输出
found a match in group 3

注意这种方法最适合不超过CPU自然大小的值。在现代通常是64。使用C++ 11中的Template可以解决此问题。以下是相同的程序:

程序3:

C++

// C++ program for comparing variable
// with multiples variable
#include 
#include 
#include 
using namespace std;
 
template 
 
// Function that checks given variable
// v in the list lst[]
bool is_in(const T& v,
           std::initializer_list lst)
{
    return (std::find(std::begin(lst),
                      std::end(lst), v)
            != std::end(lst));
}
 
// Driver Code
int main()
{
    // Number to be compared
    int num = 10;
 
    // Compare with multiple variables
    if (is_in(num, { 1, 2, 3 }))
        cout << "Found in group" << endl;
    else
        cout << "Not in group" << endl;
 
    // Character to be compared
    char c = 'a';
 
    // Compare with multiple variables
    if (is_in(c, { 'x', 'a', 'c' }))
        cout << "Found in group" << endl;
    else
        cout << "Not in group" << endl;
 
    return 0;
}
输出
Not in group
Found in group

注意虽然当不与基本类型一起使用时,效率不是很高。对于std ::字符串,它将产生一个错误。由于C++ 17带有Fold Expression ,因此此任务变得非常容易。通常将其称为“折叠” ,它有助于以更少的代码表达相同的想法,并且适用于任何数据类型。在这里, “ ||”运算符可将所有布尔结果简化为一个,如果所有比较结果均为False ,则只有False 。以下是相同的程序:

计划4:

C++

// C++ program for comparing variable
// with multiple variables
#include 
#include 
#include 
#include 
using namespace std;
 
template 
 
// Function that checks given variable
// first in the list t[]
bool is_in(First&& first, T&&... t)
{
    return ((first == t) || ...);
}
 
// Driver Code
int main()
{
    // Number to be compared
    int num = 10;
 
    // Compare using template
    if (is_in(num, 1, 2, 3))
        cout << "Found in group" << endl;
    else
        cout << "Not in group" << endl;
 
    // String to be compared
    string c = "abc";
 
    // Compare using template
    if (is_in(c, "xyz", "bhy", "abc"))
        cout << "Found in group" << endl;
    else
        cout << "Not in group" << endl;
 
    return 0;
}

输出:

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”