Thread: Can "switch" based on std::string?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Can "switch" based on std::string?

    Code:
    string x = "test1";
    switch(x){
    case "test1" : break;
    case "test2" : break;
    default : break;
    }
    Why this doesn't work for string?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because switch only uses integer constants, and "test1" is not an integer constant.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    but switch can also base on char, not only an integer

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's because char is an integer.

    Edit: and by that I mean, that is an "integral type" -- the integral types are char, short, int, long, long long, and their unsigned counterparts. All of these are integers, as stored in memory.
    Last edited by tabstop; 02-19-2008 at 06:01 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    as well as enum
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If the strings you want to switch on are part of a set, you could create an enum of the same set, then create a StringToEnum() function which converts the string you pass to it into an enum. Then you can just do your switch on the enum.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You can map strings to enumerations to alleviate your issue and also provide a common place where the string literals are defined.

    Header
    Code:
    enum TestStringIDs
    {
       TEST_ID1,
       TEST_ID2,
       END_OF_IDs
    }
    
    struct StringToID
    {
       TestStringIDs  ID;
       std::string Text;
    }
    Source
    Code:
    StringToID StringToIDBridge [] = {
    {TEST_ID1,"Test string 1"},
    {TEST_ID2,"Test string 2"} };
    This is great for mapping string literals to enumerations and can easily be loaded from a config file later. One string compare can be done within a loop and then the ID can be retrieved or vice versa. StringToID::END_OF_IDs is so that you can iterate the enumeration from 0 to END_OF_IDs.

    Just food for thought.
    Last edited by VirtualAce; 02-20-2008 at 12:12 AM.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Bubba View Post
    You can map strings to enumerations to alleviate your issue and also provide a common place where the string literals are defined.

    Header
    Code:
    enum TestStringIDs
    {
       TEST_ID1,
       TEST_ID2,
       END_OF_IDs
    }
    
    struct StringToID
    {
       TestStringIDs  ID;
       std::string Text;
    }
    Source
    Code:
    StringToID StringToIDBridge [] = {
    {TEST_ID1,"Test string 1"},
    {TEST_ID2,"Test string 2"} };
    This is great for mapping string literals to enumerations and can easily be loaded from a config file later. One string compare can be done within a loop and then the ID can be retrieved or vice versa. StringToID::END_OF_IDs is so that you can iterate the enumeration from 0 to END_OF_IDs.

    Just food for thought.
    Maybe a std::map<TestStringIDs, std::string> would be a better choice then?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. Improving my code
    By rwmarsh in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2006, 11:18 AM
  3. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  4. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  5. returning std::string
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2001, 08:31 PM