By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

enums as values in std::map

P: n/a
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,codecodetta;
map<string,another_codecoda;
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,

Mar 20 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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,codecodetta;
map<string,another_codecoda;
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
Mar 20 '07 #2

P: n/a
On Mar 20, 9:38 am, "aiooua" <aio...@gmail.comwrote:
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,codecodetta;
map<string,another_codecoda;
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 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.