Thread: Free program I'm sharing: ConvertEnumToStrings

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since you want to map enumerators to strings, C_ntua's example should actually be:
    Code:
    enum name {
        Mike = 0,
        John
    };
    
    // ...
    
    map<name, string> enumName;
    enumName.insert(make_pair(Mike, "Mike"));
    enumName.insert(make_pair(John, "John"));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    We are all saying unsigned int b/c enums are really unsigned int's under the hood. They are not 'technically' the same thing to the compiler as laserlight's example shows.

    Here is a really poorly named map that maps ints to strings. No dynamic memory allocation in my class. That is not to say the map under the hood is not dynamically allocating memory but I really don't care b/c the beauty of the STL is...um...that it works and it's all transparent to me. There are special cases when using pointers in containers but this example doesn't require any of that.

    Code tested and compiled in MSVC 2005.
    Last edited by VirtualAce; 03-12-2011 at 11:41 AM.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bubba
    We are all saying unsigned int b/c enums are really unsigned ints under the hood. They are not technically the same thing to the compiler as laserlight's example shows.
    Strictly speaking, the underlying type of an enumerator value is an implementation defined choice that depends on all the enumerators in an enumeration. This is why I chose to use the enumeration type itself rather than unsigned int in my example. Then there is also the issue readability: it is kind of silly to use magic numbers when one of the reasons for using an enum is to have named constants.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by StainedBlue View Post
    note, if this were the last enum value, no comma should be there
    Not so fast...

    Yes logically a comma separates things and there is no need to separate the last thing from the nothing that follows.
    However, this is intentionally accepted by the compiler and for good reason:
    This is because it makes it easier to add an additional value later. Specifically since you don't have to alter the previous line to add the comma, a version control system doesn't indicate a change was made to the previous line. It also means reordering lines is easier as you don't have to worry about one line not having a comma.
    It is very common and is indeed even considered good practice by many. It is done both with enums and array initialisation data.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    Yes logically a comma separates things and there is no need to separate the last thing from the nothing that follows.
    However, this is intentionally accepted by the compiler and for good reason:
    To be honest, I thought the same, but when checking, I realised that I was wrong: unlike aggregate initialiser lists, a trailing comma is not allowed for enumeration declarations. This is why I removed that comma in my example.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by iMalc View Post
    Not so fast...

    Yes logically a comma separates things and there is no need to separate the last thing from the nothing that follows.
    However, this is intentionally accepted by the compiler and for good reason:
    This is because it makes it easier to add an additional value later. Specifically since you don't have to alter the previous line to add the comma, a version control system doesn't indicate a change was made to the previous line. It also means reordering lines is easier as you don't have to worry about one line not having a comma.
    It is very common and is indeed even considered good practice by many. It is done both with enums and array initialisation data.
    For the record, stainedBlue was quoting what I said in the first post, and it seems having a comma after the last enum value in an enum doesn't work in all cases, because I've tried it before with the gcc compiler in Ubuntu, and it wouldn't compile. That is why I assumed that you're always not supposed to put a comma after the last enum value.

    @Bubba: I can't tell if you're recommending that I change my program to use maps, or if you're recommending to use maps in place of my program. I'm willing to change my program to use maps, but I don't intend to use maps instead of my program.

    And I don't care what you do with maps...it doesn't change the fact that you still end up having to manually type up the string names of the enum values if you don't use my program.

  7. #22
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by C_ntua View Post
    Without looking exactly what your code does, you could use a map like
    Code:
    enum name{
        Mike = 0,
        John,
    };
    ...
    map<string,int> enumName;
    map.insert(par<string,int>(0, "Mike");
    map.insert(par<string,int>(1, "John");
    then make a class that contains the enum and the map. You will make sure it is read-only if you want and static. You could use it like
    Code:
    Name x = Name::Mike;
    cout << x.str() << endl;
    where x.str() could return the string. To do so it would do something like
    Code:
    string& str()
    {
        return this.innerMap[this.value];
    }
    and of course, you would overload the = operator so it assigns the value given to the "this.value". Where value would be an "enum name" and innerMap the map you use to map the string with the int value of the enum.

    So you could create an "Advanced Enum" in order to have the additional functionality you want. The program will replace an enum with the advanced class.
    Quote Originally Posted by laserlight View Post
    Since you want to map enumerators to strings, C_ntua's example should actually be:
    Code:
    enum name {
        Mike = 0,
        John
    };
    
    // ...
    
    map<name, string> enumName;
    enumName.insert(make_pair(Mike, "Mike"));
    enumName.insert(make_pair(John, "John"));
    I was unable to find anything in the map reference called either "par" or "make_pair"...

  8. #23
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Programmer_P View Post
    And I don't care what you do with maps...it doesn't change the fact that you still end up having to manually type up the string names of the enum values if you don't use my program.
    Sure, but you could write a similar program to do the same thing with maps. That you decided to do something a certain way is not a logical defence of the strategy -- that's circular.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #24
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Programmer_P View Post
    I was unable to find anything in the map reference called either "par" or "make_pair"...
    A pair is a separate (but related) datatype. Here's how you use it with map:
    Code:
    	map<string,int> test;
    	test.insert(pair<string,int>("this",4));
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #25
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by MK27 View Post
    Sure, but you could write a similar program to do the same thing with maps. That you decided to do something a certain way is not a logical defence of the strategy -- that's circular.
    Did I not just say I was willing to change my program to use maps...?

    Quote Originally Posted by Programmer_P View Post
    @Bubba: I can't tell if you're recommending that I change my program to use maps, or if you're recommending to use maps in place of my program. I'm willing to change my program to use maps, but I don't intend to use maps instead of my program.

  11. #26
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by MK27 View Post
    A pair is a separate (but related) datatype. Here's how you use it with map:
    Code:
    	map<string,int> test;
    	test.insert(pair<string,int>("this",4));
    Thanks.
    Is the "pair" definition in the same header as map?

  12. #27
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Pair and make_pair is in <utility>. Include that just to be sure.

  13. #28
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Thumbs up

    Quote Originally Posted by whiteflags View Post
    Pair and make_pair is in <utility>. Include that just to be sure.
    Ok, thank you.

  14. #29
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Programmer_P View Post
    Did I not just say I was willing to change my program to use maps...?
    What's done is done, obviously. I have code that works that I frequently use that I'd still love to re-write to work better and/or differently, but I don't have time. I'm sure that's true of most people.

    I think the idea here was not necessarily to force you to scrap everything and start again, but more to point out the possibilities that you missed so you are aware of them in the future.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #30
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by MK27 View Post
    What's done is done, obviously. I have code that works that I frequently use that I'd still love to re-write to work better and/or differently, but I don't have time. I'm sure that's true of most people.

    I think the idea here was not necessarily to force you to scrap everything and start again, but more to point out the possibilities that you missed so you are aware of them in the future.
    Yeah, but it wouldn't take me that long to change it to use maps instead if I wanted to, then re-upload it, and get Bubba to change the download link. Like already mentioned, I'm willing to do this if its such a big issue.
    But my program works, as is, right now.

    I do understand people's misconceptions about the program though. I forgot to mention in the first post that the program does more than just converts any enum. What my program does that makes it more valuable than that is it provides a function which returns a reference to a vector of enum value strings (automatically generated by my program). This makes it possible to get all the enum value names without typing them up. Thus, if you have a really long enum, for example, which has many enum values, and you don't want to type up each string name of those enum values, you can use my program to do it for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. help sharing a program with friends...
    By fingerlickin in forum C# Programming
    Replies: 1
    Last Post: 11-05-2005, 09:54 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Making a program to free up Ram memory
    By cfrost in forum Windows Programming
    Replies: 1
    Last Post: 10-03-2004, 06:52 AM