enums as values in std::map

A

aiooua

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,
 
F

Fei Liu

aiooua said:
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
 
A

AnonMail2005

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top