• Advertisement

Archived

This topic is now archived and is closed to further replies.

switch with strings?

This topic is 5642 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It''s is possible to use switch statment with strings, for example: switch (strName) { case "Eric": ... [statement] default: ... [statement] }

Share this post


Link to post
Share on other sites
Advertisement
No.

What I did, is create a simple hashing function that gave a good spread for strings, and then I created a little cmd line app that printed out the code I needed to use for strings.

For example:

c:\>hasher this is a test
case 1291: // this
break;
case 139: // is
break;
case 193: // a
break;
case 1574: // test
break;


Then I just copy and paste that into my source
like this:


  
switch(hash_string(somestring.c_str())
{
case 1291: // this

break;
case 139: // is

break;
case 193: // a

break;
case 1574: // test

break;
};


Hope that helped

[edited by - daerid on September 9, 2002 6:23:02 PM]

Share this post


Link to post
Share on other sites
Have you thought about using a std::map<std::string, T>

It''s not a speed deamon, but it gets the job done quickly & correctly.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]

Share this post


Link to post
Share on other sites
(off topic)

Can we please remember the semicolons on our <s? Otherwise, they show up as &lt under non-IE browsers.

Share this post


Link to post
Share on other sites

  • Advertisement