printing enums

This is a discussion on printing enums within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Daved A switch would still be preferable to a map. It would not break, and would avoid ...

  1. #16
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by Daved
    A switch would still be preferable to a map. It would not break, and would avoid logarithmic time lookups and extra storage in favor of constant time lookup. To make an array work with consecutive enum values that don't start at 0 would be as simple as adding an offset.

    Only if the strings were variable would I consider using a map, and then an unordered_map (hash_map) would be preferable if you had one available.
    Well, this argument is really pointless. A switch would work fine... I'm not denying that. Of course, a map may theoretically be less efficient, but there are a few things to consider:
    a) Even if you look up the name occasionally (i.e. more than once), is it really going to be often enough to make a difference?
    b) Are you really going to have enums that are so broad in their range of values that this matters?
    c) As an enum is fixed at compile time, and cannot scale, the fact that searching a map scales as lg really means nothing.
    d) There are other capabilities of maps that might make them more desirable (one of them being that they play nice with the rest of STL).

    So, in conclusion to my contributions to this thread, if you want to stick a switch in a function that returns a string (or other identifier), that is fine (I never said otherwise). A std::map would also work quite handily. Don't use an array though, as you could very easily break it (perhaps it would only mean an offset, perhaps a function to map enum values to array indices -- it is still not easily maintainable code).


    And JoshR is correct. Enums must be set to constant values.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I don't consider it to be an argument. I love std::maps. I use them all the time. They would work fine here, I just don't think they make as much sense as other alternatives.

  3. #18
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    Code:
    enum { one = 1, two = 1 };
    Which would you print "one" or "two" ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #19
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    "three", of course.

    Touché
    Last edited by Zach L.; 08-09-2005 at 06:30 AM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #20
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    const int NrOfTypes = 3;
    
    enum TYPE
    {
      TYPE_ONE,
      TYPE_TWO,
      TYPE_THREE,
    };
    
    const char* const TypeList[NrOfTypes] =
    {
      "One",
      "Two",
      "Three",
    };
    
    std::cout << TypeList[TYPE_TWO];
    (doesn't work well with custom enum values though)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Printing Lines to .txt File
    By Programmer3922 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 12:45 PM
  3. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  4. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 07:10 PM
  5. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 03:46 AM

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