Std :: map default value for enumerations

Say we have:

enum X { X1, X2, X3 }; int func() { std::map<int, X> abc; ... } 

Suppose 0 is a key that is not in the container.

I know that abc [0] needs to initialize an X object.

Here are the questions:

(1) Will initialization always be zero initialization for enums? namely abc [0], is always initialized as an enumerator corresponding to 0?

(2) What if we have

 enum X { X1 = 1, ... 

What will abc [0] be?

+5
source share
1 answer

Will initialization always be zero initialization for enums? namely abc[0] always initialized as an enumerator corresponding to 0 ?

Yes.

What if we have

 enum X { X1 = 1, ... 

What will abc[0] be?

It will be 0 .

Work program (also can be seen at http://ideone.com/RVOfT6 ):

 #include <iostream> #include <map> enum X { X1, X2, X3 }; int main() { X x = {}; std::map<int, X> abc; std::cout << x << std::endl; std::cout << abc[0] << std::endl; } 

Conclusion:

  0
 0
+1
source

All Articles