A Very Long List of OR...

Hello guys,
I want to write a command like this:

int a;
if (a == 0) || (a == 1) || (a == 3) or 15 or 27 or 36 or... (a list of random numbers)... or (a == 360) cout << "blah blah blah";

The problem is there there are perhaps 30 ORs involved... Is there anyway for me to write this more concisely instead of exhausting 30s brackets and 30s ORs?

Many thanks.

Regards,
Bosco
1
2
3
4
5
6
int targetVal = 49;
vector<int> nums = {1, 3, 15, 27, 29, 42,49, 71, 73, 91};
if (find(nums.begin(), nums.end(), targetVal) != nums.end())
{
    // Do something
}
Thanks Rollie,
Concerning the vector here... when you use the large bracket how are the elements assigned to the nums vector? Is that 1 as the first number inside the bracket assigned to nums[0] and 91 as the tenth and last number in the bracket assigned to nums[9]? Or are the numbers assigned to the elements of vectors in other ways?
Yes that is how they are assigned. This syntax to initialize a vector is new in C++11 and therefore not supported in older compilers.
If you don't want to use vectors...

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main() {
    int x = 5; //this is whatever value you're comparing
    int list[] = {0, 1, 3, 15, 27, 36, 55, 60, 91, 205};
    for each(int a in list) {
        if (x == a) {
            //do whatever
        }
    }
}
Thanks guys... and Peter87 is the compiler of Dev C++ one of the older compilers then??
if you don't want to use vector, you can do so:
1
2
3
4
5
6
7
int targetVal = 49;
int nums = {1, 3, 15, 27, 29, 42,49, 71, 73, 91};
const int *nums_end = nums + (sizeof(nums) / sizeof(*nums));
if (find(nums, nums_end, targetVal) != nums_end)
{
    // Do something
}
boscomanilow128 wrote:
is the compiler of Dev C++ one of the older compilers then??
Yes, it certainly is.

@coder777

Instead of

1
2
const int *nums_end = nums + (sizeof(nums) / sizeof(*nums));
if (find(nums, nums_end, targetVal) != nums_end)


it is much better to write

if ( std::find( std::begin( nums ), std::end( nums ), targetVal ) != std::end( nums ) )
vlad from moscow wrote:
it is much better to write
hm, yes. There're many things better in the C++11 world
hmm ok thank you all
Topic archived. No new replies allowed.