Can "switch" based on std::string?

This is a discussion on Can "switch" based on std::string? within the C++ Programming forums, part of the General Programming Boards category; Code: string x = "test1"; switch(x){ case "test1" : break; case "test2" : break; default : break; } Why this ...

  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,185
    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,185
    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 05:01 PM.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    as well as enum
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    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-19-2008 at 11:12 PM.

  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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21