Just playing with ideas here, it feels like this isn't the best way to do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
int wd = 678;
int wdd[3] = {012, 345, 678};
for (int i=0; i<3; i++)
{
if (wd==wdd[i])
{
switch (i)
{
case 0:
cout << "one";
break;
case 1:
cout << "two";
break;
case 2:
cout << "three";
break;
}
}
}
| |
Output:
Or a variation of the above, pull variable i outside the for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
int i;
for (i=0; i<3; i++)
if (wd==wdd[i])
break;
switch (i)
{
case 0:
cout << "one";
break;
case 1:
cout << "two";
break;
case 2:
cout << "three";
break;
default:
cout << "not found";
}
| |
Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
int wd;
int wdd[3] = { /* some values */ };
switch ( std::distance( std::begin( wdd ), std::find( std::begin( wdd ), std::end( wdd ), wd ) ) )
{
case 0:
std:;cout << "zero";
break;
case 1:
std::cout << "first";
break;
case 2:
std::cout << "second";
break;
default:
std::cout << "Not found";
break;
}
| |
Last edited on
Vlad, what header does that use?
#include <algorithm>
#include <iterator>
and of course
#include <iostream>
provided that your compiler supports the C++ 2011 standard regarding to functions std::begin and std::end. They can be simply substituted for
wdd and wdd + 3 correspondingly.
So you can rewrite the switch as
switch ( std::distance( wdd, std::find( wdd, wdd + 3, wd ) ) )
Last edited on