Like Tree5Likes

Enum as array

This is a discussion on Enum as array within the C++ Programming forums, part of the General Programming Boards category; I have this problem. I have a hashtable and I would like to use enum to know which position is ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    Enum as array

    I have this problem. I have a hashtable and I would like to use enum to know which position is occupied and which one had its element deleted.
    My enum is this:
    Code:
    Status status;
    enum Status{occupied,deleted};
    Now, I would like to go through the table and check if a position is occupied or deleted.
    This is the idea I have in mind.
    for(int i = 0; i < tableSzite; ++)
    if(status != occupied) //do this...
    if(status == deleted)// do this...

    But for this I will need an enum at least as large as tableSize, and I only have two status. I am just stuck here, any help is appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,871
    Make status an array of enum Status.
    Code:
    enum Status{occupied, deleted};
    
    Status status[max_tableSzite];
    
    for (int i = 0; i < tableSzite; ++i)
    {
         if (status[i] != occupied) .....
    }
    Given that you only have two values for status, which are mutually exclusive, a simple boolean can be used instead of enum Status.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Thanks, that seems like a good idea. My question is when you write Status status[max_tableSzite]; what does this array contain. Does it contain equal numbers of occupied and deleted? Because when we say status[i], it is vital to know what status contains at that time.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    And I don't think a boolean will help because boolean only has true and false. So, for example I can have a boolean that tells me if a position is occupied or not but not one that can tell me if a position is unoccupied but is deleted, without having a second boolean value that stores for me the deleted position. While I don't know enums well, I was thinking with an enum of occupied and deleted, I would be able to go through my entire array and tell which position is occupied and which one is deleted.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,976
    grumpy's point about using a boolean was that in your initial example, there were really only two states. If it turns out that you have more or were catering for more, than an enum would be appropriate.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    Quote Originally Posted by Dontgiveup View Post
    Thanks, that seems like a good idea. My question is when you write Status status[max_tableSzite]; what does this array contain. Does it contain equal numbers of occupied and deleted? Because when we say status[i], it is vital to know what status contains at that time.
    Array elements will have undetermined initial values unless you initialize the array. Basically it means if you just define the array as opposed to initializing it, no matter what type it is, you won't know the initial values. You can initialize, say, the first element, and the rest will be 0 (or "occupied" as far as Status is concerned).

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,266
    If there are more than two states then mention them all:
    Code:
    enum Status { unoccupied, occupied, deleted };
    Otherwise it would be the case that regardless of what you might think that a boolean would be appropriate.
    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"

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Thanks everyone for your reply. OK, I have now decided that I need 3 states and two won't be enough, so I guess that means an enum is more efficient and better in this case. Only problem is I haven't used enum before, so I don't know how to use it. If I had a boolean array, I would have gone ahead this way (since it is bool, just two states):

    Code:
    bool occupied [];
    
    //fill array with default values, to avoid it holding undefined values
    
    for(int i = 0; i < tableSize; ++i)
    occupied[i] = false;
    
    
    //then later change each index as you fill in each place in the //hashtable, e.g. change value of index 5 to true:
    
    occupied[5] = true;
    Now, how can I do the same with enums wit 3 states. I have declared the enum with 3 states thus:
    Code:
    enum States{
    		occupied,
    		free,
    		deleted
    	};
    
    //Then declare it as:
    
    States states;
    now how can I "fill" them with default values, say occupied, like was the case with the boolean array? I can't do it like this since states has only 3 values, so then index is from 0 to 3 but my tableSize can be as big as 100,000. So, unlike in the boolean case, I can't do this.

    Code:
    for int i = 0; i < tableSize; ++i;
    states[i] = occupied;
    
    //and then later change each state as it changes.
    So, how could this be done with enum? Thanks in advance.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Alright. I have just been experimenting around, and I found something rather strange.

    Code:
    int values[10];
        for(int i = 0; i < 10; ++i)
        values[i] = i;    
        Status status;
        for(int i = 0; i < 3; ++i)
        values[i] = occupied;
        
        for(int i = 0; i < 10; ++i)
        if(!(values[i] == occupied)) cout << values[i];
        int k;
        cin >>k;
    This code not only compiles but actually seems to produce the results I desired. But I don't understand. I mean values[i] is an int whereas occupied is an enum, so how can that be possible. And is this true for all types, not just primitive ones. So if my values held car objects instead of primitive int, would I be able to say values[i] = occupied? This really seems easy and wonderful, if it works. It is all I have been looking for, basically. Will try to experiment with non-primitive types and see if it works. But I still don't get how you can assign arrays containing two different types to each other.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Ok, tried it with non-primitives, and it won't work because you have to define what = mean for non-primitives (overloading = operator). But another idea befell on me, which seems to also work alright. By having an instance of State in a class and then using that, it seems to work, something like.

    Code:
    enum States {
    occupied,
    unoccupied,
    deleted
    };
    
    class Person{
    
    public:
    int id;
    State states;
    
    Person (): id(1) {};
    
    };
    
    int main() {
    Person p [10];    
        for(int i = 0; i < 10; ++i)
        p[i].id = i;       
        for(int i = 0; i < 3; ++i)
        p[i].states= occupied;
        
        for(int i = 0; i < 10; ++i)
        if(!(p[i].states== occupied)) cout << p[i].id;
    
    }
    something like that. But I don't know whether this kind of idea is good, performancewise, if I have a hashtable that can have up to 100,000 values, and will also be resized.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    OK. I have got yet more problem now. I want to initialise the enum with default values, but only with one enum value, namely unoccupied. So, will this work? The idea is to make sure that in the beginning all places in the array are by fault unoccupied. So, expanding my constructor thus is fine I guess (seems to work with the test I did, so far):

    Code:
    Person (): id(1), states(unoccupied) {};

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    4 posts asking ostensibly the same question...

    This is how you initialize arrays of enums:
    Quote Originally Posted by whiteflags View Post
    Array elements will have undetermined initial values unless you initialize the array. Basically it means if you just define the array as opposed to initializing it, no matter what type it is, you won't know the initial values. You can initialize, say, the first element, and the rest will be 0 (or "occupied" as far as Status is concerned).
    It's exactly alike arrays of other things.
    Code:
     State example[10] = {occupied};
    You already know how to do it within a class because of the code that you showed. This is the default constructor of Person:
    Code:
    Person (): id(1), states(unoccupied) {};
    which means if you make an array of Person objects, each person will start out with an id of 1 and a status of unoccupied.
    Last edited by whiteflags; 06-14-2012 at 04:16 PM.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Thanks. You wrote it's exactly like any other array. So, in your example we have example, which contains ten elements, each occupied:
    Code:
    State example[10] = {occupied};
    now, if I write a loop, like:

    Code:
    for(int i = 0; i < 5; ++i)
    example[i] = unoccupied;
    I get the error: invalid types `State[int]' for array subscript

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    It works for me.
    http://codepad.org/8MeKVFke

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    OK, it was my mistake, sorry. Forgot to specify the array size.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning enum types to enum types?
    By see the big C in forum C Programming
    Replies: 10
    Last Post: 12-21-2010, 01:32 AM
  2. enum and 2D array declaration
    By bigbig in forum C Programming
    Replies: 2
    Last Post: 03-30-2010, 11:59 AM
  3. enum constants as array indices
    By hzmonte in forum C Programming
    Replies: 2
    Last Post: 01-23-2006, 07:23 PM
  4. enum Help
    By Bitphire in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2004, 10:03 AM
  5. What enum got to do with it?
    By correlcj in forum C Programming
    Replies: 4
    Last Post: 07-18-2002, 08:12 PM

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