0

I was wondering if there is an easy way to write this if statement in c++.

string c="B";
if(c=="B"||c=="X"||c=="I")
{
//stuff
}

for example,

string c="B";    
if(c in {"b","X","I"})
{
//stuff
}
Joao Noch
  • 179
  • 8
  • There aren't any *good* ways to do it built into the language. You could place all the different values you want to check against into a data structure (e.g. an unordered_set) and then check to see if your string is a present in the data structure. Alternatively, there are various ugly ways to do it at https://stackoverflow.com/questions/4165131/c-c-switch-for-non-integers – Jeremy Friesner Jun 22 '17 at 02:26
  • Putting numbers into a set then checking for membership is a personal favorite of mine. If the items you're checking against are always the same, just pre-create the set ahead of time. – Carcigenicate Jun 22 '17 at 02:31
  • Possible duplicate of [What is the C++ equivalent of Python's "in" operator?](https://stackoverflow.com/questions/44622964/what-is-the-c-equivalent-of-pythons-in-operator) –  Jun 22 '17 at 04:24

3 Answers3

1

There is no direct support in language for this, but you can emulate it using function. For example, let us define a function that accept a string and a vector of strings to be compared:

bool in(const std::string& s, std::vector<std::string> v)
{
    for (auto&& i : v)
        if ( s == i)
            return true;
    return false;
}

now you can use this function directly in you if statement, using an initilizer list:

int main()
{
    std::string c = "B";
    if ( in(c, {"C","X","I", "B"}) )
        std::cout << "found\n";
    else
        std::cout << "not found\n"; 
}
Amadeus
  • 10,199
  • 3
  • 25
  • 31
1

You can use the std:: find function to search your array.Suppose your array is arr=["C","X","I"] : tofind string c="C" For example your statement will change to:-

  if(find(arr.begin(),arr.end(),c)!=arr.end())
   {
   //found do something

    }

There is no "in" in C++

0

If doing multiple searches, std::set or std::unordered_map (C++11 hash table) will be better performance-wise than linear search through an array with std::find.

std::set<std::string> data_set{"B","X","I"};
//...somewhere else
bool has_c = data_set.count(c)>0;

Both std::set and std::unordered_maphave a count function that makes testing for a value pretty clean looking, i.e. avoiding iterators.

Program

Here's a full working program,

#include <string>
#include <set>
#include <iostream>
using namespace std;
int main()
{
    set<string> data_set{"C","X","I"}

    //...somewhere else
    string c="B";
    if( data_set.count(c)>0 )
    {
        cout << "here\n";
    }
    else
    {
        cout << "not\n";
    }
}

Don't forget to set C++11 standard when you compile, e.g. g++ -std=c++11 main.cc.

wawiesel
  • 170
  • 9
  • 1
    As slick as that looks, I'm pretty sure there's no performance benefits to creating a set just to search it one time. – Benjamin Lindley Jun 22 '17 at 04:19
  • Edited according to @BenjaminLindley comment to be clearer about performance gain only happening if you store the set and do multiple checks. – wawiesel Jun 22 '17 at 12:49