GameX
GameX

Reputation: 67

Easier way for multiple if-else tests

I have a string and I want to execute if statement if string value matches given set of words(say ape,eat,sleep,mango........)

I can do:-

if(name=="ape" || name=="eat".............)

Is there any easier method to do this?

I want to use if-else ONLY.

Upvotes: 0

Views: 135

Answers (5)

Gem Taylor
Gem Taylor

Reputation: 5613

If you don't care about performance but want minimal code, build a single std::string containing all the words split by a distinct separator character, also at the start and end. Then std::string::find() your search word also wrapped in the separator character:

static const std::string words("^ape^stuff^dude^");
if (words.find("^"+name+"^") != words.npos)
    std::cout << "found it";

Or dispense with the const std::string and use strstr from the clib.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310920

Decalare an array of the words and then use either the standard algorithm std::find or std::any_of.

for example

const char * words[] =
{
    "ape", "eat", "sleep", "mango"
}; 

if ( std::find( std::begin( words ), std::end( words ), name ) != std::end( words ) )
{
   //...
}

if you will declare a sorted array then you can use the standard algorithm std::binary_search.

Here is a demonstrative program

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

int main() 
{
    const char * words[] =
    {
        "eat", "ape", "mango", "sleep" 
    };

    std::string name( "mango" );

    if ( std::binary_search( std::begin( words ), std::end( words ), name ) )
    {
        std::cout << name << " is a correct name\n";
    }

    return 0;
}

Its output is

mango is a correct name

Or place the words in a standard container as for example std::set and use the find method of the container.

Upvotes: 2

sbecker
sbecker

Reputation: 584

You could use an unordered_set of string and search for the name. Something like this:

 #include <iostream>
 #include <unordered_set>
 #include <string>
 #include <algorithm>

 int main()
 {
     std::unordered_set<std::string> values = { "ape", "stuff", "dude" };


     std::string name = "ape";

     if (std::find(std::begin(values), std::end(values), name) != std::end(values)) {
         std::cout << "found it";
     } else {
         std::cout << "no found it";
     }

     return 0;
 }

Thanks Sombrero Chicken for mentioning the unordered_set. I defaulted to vector. There might be some C++17/C++20 version with constexpr unordered_set. But I leave this to someone else.

Upvotes: 0

bipll
bipll

Reputation: 11940

for(auto pattern: {"ape", "eat"}) {
   if(name == pattern) {
      // all the consequences here
      break;
   }
}

Alternatively, on a hot path, you could use something like a hash set:

static const std::unordered_set<std::string> words{"ape", "eat"};
if(words.find(name) != words.end()) {
}

Just make sure it's a static const, to not reinitialize it every time anew. It's probably better if you have a real huge set of patterns to allow.

Upvotes: 1

Hatted Rooster
Hatted Rooster

Reputation: 36463

There isn't if you want to keep it to only if and else without anything added.

Otherwise you could use a std::unordered_set, fill it with those words and use find().

Upvotes: 2

Related Questions