C ++ does not offer syntax switchfor mapping ranges.
When the ranges are relatively small, you can put mark labels and rely on failure:
switch(i) {
case 20:
case 21:
case 22:
case 23:
case 24:
case 25: doSomething();
break;
case 26:
case 27:
case 28:
case 29: doSomethingElse();
break;
...
}
For medium-sized ranges (1000 elements or so), you can use a vector of function objects to send to specific logic, but this requires a lot more work than writing a simple statement switch.
For large ranges, the best choice is the operator chain if- else.
source
share