std::map 枚举的默认值
std::map default value for enums
假设我们有:
enum X {
X1,
X2,
X3
};
int func() {
std::map<int, X> abc;
...
}
假设 0 是不在容器中的密钥。
我知道 abc[0] 需要对 X 对象进行值初始化。
问题如下:
(1) 枚举的初始化总是零初始化吗?即abc[0]总是初始化为0?
对应的枚举数
(2) 如果我们有
enum X {
X1 = 1,
...
abc[0] 会是什么?
Will the initialization always be zero-initialization for enumerations? namely abc[0]
is always initialized as the enumerator corresponding to 0
?
是的。
What if we have
enum X {
X1 = 1,
...
What will abc[0]
be?
它将是0
。
工作程序(也可以在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;
}
输出:
0
0
假设我们有:
enum X {
X1,
X2,
X3
};
int func() {
std::map<int, X> abc;
...
}
假设 0 是不在容器中的密钥。
我知道 abc[0] 需要对 X 对象进行值初始化。
问题如下:
(1) 枚举的初始化总是零初始化吗?即abc[0]总是初始化为0?
对应的枚举数(2) 如果我们有
enum X {
X1 = 1,
...
abc[0] 会是什么?
Will the initialization always be zero-initialization for enumerations? namely
abc[0]
is always initialized as the enumerator corresponding to0
?
是的。
What if we have
enum X { X1 = 1, ...
What will
abc[0]
be?
它将是0
。
工作程序(也可以在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;
}
输出:
0 0