问题描述
最近,我对std :: map运算符[]函数感到困惑。
在MSDN库中,它说:如果未找到参数键值,那么它将与数据类型的默认值一起插入。
我试图搜索更准确的解释这个问题。例如:
在此页面中, Michael Anderson说默认值由默认构造函数(零参数构造函数)构造。
现在我的任务是:内置类型?。它是编译器相关吗?或者c ++ stardard委员会有这个问题的标准吗?
我在visual studio 2008做了一个测试,为int类型,并找到int类型的值为0。
这是在标准中定义的,是的。在这种情况下映射正在执行默认初始化。
对于内置类型,在'98标准中,请参见第8.5节初始化器 :
之前,
标量类型有:
- 算术类型(整数,浮点数)
- 枚举类型
- 指针类型
- 指向成员类型的指针
Recently, I was confused by the std::map operator[] function.In the MSDN library, it says: "If the argument key value is not found, then it is inserted along with the default value of the data type."I tryed to search much more exactly explanation for this issue. For example here:std::map default valueIn this page, Michael Anderson said that "the default value is constructed by the default constructor(zero parameter constructor)".
Now my quest comes to this:"what the default value for the build-in type?". Was it compiler related? Or is there a standard for this issue by the c++ stardard committee?
I did a test on visual studio 2008 for the "int" type, and found the "int" type is construted with the value 0.
This is defined in the standard, yes. map is performing "default initialization" in this case. As you say, for class types, that calls a no-arguments constructor.
For built-in types, in the '98 standard, see section 8.5, "Initializers":
And, previously,
Scalar types are:
- Arithmetic types (integer, floating point)
- Enumeration types
- Pointer types
- Pointer to member types
In particular, the behaviour you see with an integer (initialized to zero) is defined by the standard, and you can rely on it.
这篇关于std :: map内置类型的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!