I saw this C# (C sharp see code below) example given by Stefan ...?? (forgot his last name), he gave a solution for implementing a switch on strings using STL and pure C++.
I will not show his solution because its rather large compared to the C# solution.
My questions :
Why does C++ not have a "standard" solution for such a common thing as a switch on strings, is there a good reason?
Is there a "easy" solution to emulate a switch on strings in C++ without
having to use STL ?
This code below is legal C#. You can switch on strings in C#, but NOT in C++
switch(strValue)
{
case "Value X":
DoThis();
break;
case "Value Y":
DoThat();
break;
case "Value Z";
DoSomethingElse();
break;
default:
DontKnowWhatToDo();
break;
}
Greetinx and thanx from Winnitou
Comments
Because it's no more efficient than the using an if-else-if construct. With integers, the compiler (in theory) can build a lookup table mapping a particular value to the absolute offset of a particular case label, given the switch statement a significant speed advantage. With strings, the compiler has no such optimization opportinity - it will have to generate the same code as for the if-else-if block.
: Is there a "easy" solution to emulate a switch on strings in C++ without having to use STL ?
Yeah, if-else-if:
[code=ffffff]
[color=000000][b]if[/b][/color] (strcmp(strValue, [color=bb0000]"Value X"[/color]) == [color=bb0000]0[/color]) DoThis();
[color=000000][b]else[/b][/color] [color=000000][b]if[/b][/color] (strcmp(strValue, [color=bb0000]"Value Y"[/color]) == [color=bb0000]0[/color]) DoThat();
[color=000000][b]else[/b][/color] [color=000000][b]if[/b][/color] (strcmp(strValue, [color=bb0000]"Value Z"[/color]) == [color=bb0000]0[/color]) DoSomethingElse();
[color=000000][b]else[/b][/color] DontKnowWhatToDo();
[/code]
Or you can create a lookup function (essentially a hash function) that maps a particular string to an integer ID, then you can use that ID in the a switch statement. It will have pretty much the same performance as the if-else-if, but you might find it more readable:
[code=ffffff]
[color=000080]enum[/color] { VALUE_X, VALUE_Y, VALUE_Z };
[color=000080]int[/color] getid ([color=000080][b]const[/b][/color] [color=000080]char[/color]* s) {
[color=000080][b]static[/b] [b]const[/b][/color] [color=000080]char[/color]* lookup[] = { [color=bb0000]"Value X"[/color], [color=bb0000]"Value Y"[/color], [color=bb0000]"Value Z"[/color] };
[color=000080][b]static const[/b] size_t[/color] size = [color=000000][b]sizeof[/b][/color] lookup / [color=000000][b]sizeof[/b][/color] *lookup;
[color=000000][b]for[/b][/color] ([color=000080]size_t[/color] i=[color=bb0000]0[/color]; i < size; ++i)
[color=000000][b]if[/b][/color] (strcmp(s, lookup[i]) == [color=bb0000]0[/color])
[color=000000][b]return[/b][/color] i;
[color=000000][b]return[/b][/color] -[color=bb0000]1[/color];
}
...
[color=000000][b]switch[/b][/color] (getid(strValue)) {
[color=000000][b]case[/b][/color] VALUE_X: DoThis(); [color=000000][b]break[/b][/color];
[color=000000][b]case[/b][/color] VALUE_Y: DoThat(); [color=000000][b]break[/b][/color];
[color=000000][b]case[/b][/color] VALUE_Z: DoSomethingElse(); [color=000000][b]break[/b][/color];
[color=000000][b]default[/b][/color]: DontKnowWhatToDo();
}
[/code]
: This code below is legal C#. You can switch on strings in C#, but NOT in C++
Right, that's one of many, many things that makes them [italic]different languages[/italic].
Cheers,
Eric