enums as values in std::map

Discussion in 'C++' started by aiooua, Mar 20, 2007.

  1. aiooua

    aiooua Guest

    hello,

    I had a doubt regarding the handling of enums in std::map.
    Consider the following piece of code:

    ------
    #include<iostream>
    #include<map>
    using namespace std;
    typedef enum {
    UNINITIALIZED,
    MEANINGFUL_1,
    MEANINGFUL_2
    }code;

    typedef enum {
    O_UNINITIALIZED = 100,
    O_MEANINGFUL_1,
    O_MEANINGFUL_2
    }another_code;

    int main(){
    map<string,code> codetta;
    map<string,another_code> coda;
    cout<<codetta["unset"]<<endl;
    cout<<coda["unset"]<<endl;
    }
    ------

    Is it ok to assume that for both maps, the data-value returned for all
    unset key-values will -always- be 0?

    thanks for your time,
     
    aiooua, Mar 20, 2007
    #1
    1. Advertising

  2. aiooua

    Fei Liu Guest

    aiooua wrote:
    > hello,
    >
    > I had a doubt regarding the handling of enums in std::map.
    > Consider the following piece of code:
    >
    > ------
    > #include<iostream>
    > #include<map>
    > using namespace std;
    > typedef enum {
    > UNINITIALIZED,
    > MEANINGFUL_1,
    > MEANINGFUL_2
    > }code;
    >
    > typedef enum {
    > O_UNINITIALIZED = 100,
    > O_MEANINGFUL_1,
    > O_MEANINGFUL_2
    > }another_code;
    >
    > int main(){
    > map<string,code> codetta;
    > map<string,another_code> coda;
    > cout<<codetta["unset"]<<endl;
    > cout<<coda["unset"]<<endl;
    > }
    > ------
    >
    > Is it ok to assume that for both maps, the data-value returned for all
    > unset key-values will -always- be 0?
    >
    > thanks for your time,
    >

    Depending on what your code["unset"] is...your answer will be different.
    However, it's certain that: UNINITIALIZED = 0, O_UNINITIALIZED = 100

    Fei
     
    Fei Liu, Mar 20, 2007
    #2
    1. Advertising

  3. aiooua

    Guest

    On Mar 20, 9:38 am, "aiooua" <> wrote:
    > hello,
    >
    > I had a doubt regarding the handling of enums in std::map.
    > Consider the following piece of code:
    >
    > ------
    > #include<iostream>
    > #include<map>
    > using namespace std;
    > typedef enum {
    > UNINITIALIZED,
    > MEANINGFUL_1,
    > MEANINGFUL_2
    >
    > }code;
    >
    > typedef enum {
    > O_UNINITIALIZED = 100,
    > O_MEANINGFUL_1,
    > O_MEANINGFUL_2
    >
    > }another_code;
    >
    > int main(){
    > map<string,code> codetta;
    > map<string,another_code> coda;
    > cout<<codetta["unset"]<<endl;
    > cout<<coda["unset"]<<endl;}
    >
    > ------
    >
    > Is it ok to assume that for both maps, the data-value returned for all
    > unset key-values will -always- be 0?
    >
    > thanks for your time,

    I think you are correct but don't quote me on it. The right question
    is
    what is the default initialization of an enum.

    Search this group for "default initialize enum" for some relevant
    threads.
    Some quote the relevant portions of the standard.
     
    , Mar 20, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,329
    Ivan Vecerina
    Mar 17, 2005
  2. =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=

    Enums without identifier, enums and typedef

    =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=, Jan 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,141
    Keith Thompson
    Jan 20, 2007
  3. Replies:
    1
    Views:
    429
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,027
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    2,015
    James Kanze
    Dec 21, 2008
Loading...

Share This Page