One line switch

Welcome,

While I'm not paid by lines of code I'm always try to be simple and short.

The task is that we get an index and we need to know that this index is within a specific selection.

bool isInList(int index)
{
    bool result = false;
    switch (index)
    {
    case 3 :
    case 5 :
    case 7 : result = true; break;
    }

    return result;
}

Nice but to looooong...

This is simpler a bit ;)

bool isInList(int index)
{
    return std::set<int>{3,5,7}.count(index);
}

thanks for visiting.

Headline: 
C++ simple switch
Date modified: 
Monday, December 19, 2016 - 13:00
Date published: 
Keywords: 
C++,std::set,switch
Author: