I haven't done the FAQ for it yet, but the basic idea is that you must be able to convert the string to a sufficiently unique number in a consistent way.
The simplest way of doing this is an array of strings, because each string has a unique index into the array.
Finding the string in the array is the same thing as converting the string to a number:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
string names[] = { "Juan", "Hediberto", "Maria", "Luis", "Nalleli" };
string name_to_find = "Maria";
switch (std::find( names, names + 5, name_to_find ))
{
case 0: cout << "Hola Juan\n"; break;
case 1: cout << "Hola Edi\n"; break;
case 2: cout << "Bendito sea, Maria\n"; break;
case 3: cout << "What'Sapo, Luis?\n"; break;
case 4: cout << "Hey there Na-Na!\n"; break;
default: cout << "No te conozco (I don't know you)\n";
}
|
Using
std::find() is very
simple, but it also has drawbacks, particularly, if the
name_to_find is "Na-Na" then it will not find Nalleli. Nor will it find "luis", because of capitalization. A more robust solution will take care to convert all the proper inputs to the proper number, and where there is ambiguity, properly complain to the user.
[edit] Oh, yeah, as in the example you linked: The poster was demonstrating the same idea, but using a
std::map container, where the strings are keys and the integer values are the results. [/edit]
Here are two more advanced examples of converting a string to an integer value:
(This one converts an English month name into its index number -- scroll down to the second code block)
http://www.cplusplus.com/forum/general/11460/#msg54095
(This one converts boolean strings [like "yes" and "true"] into a boolean value)
http://www.cplusplus.com/forum/beginner/18518/#msg94815
Hope this helps.