Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;std::使用[]运算符初始化映射Intristic类型_C++_Stl_Stdmap - Fatal编程技术网

C++ C++;std::使用[]运算符初始化映射Intristic类型

C++ C++;std::使用[]运算符初始化映射Intristic类型,c++,stl,stdmap,C++,Stl,Stdmap,多好的标题啊,假设我有一张这样的地图: std::map<int, int> m; std::map m; 如果我写下 cout<<m[4]; cout该值将是该类型的默认构造函数创建的值,因为新的点是使用T()填充的。对于int,这是0。您可以亲自看到这一点: #include <iostream> using namespace std; int main() { cout << int() << endl; //

多好的标题啊,假设我有一张这样的地图:

std::map<int, int> m;
std::map m;
如果我写下

cout<<m[4];

cout该值将是该类型的默认构造函数创建的值,因为新的点是使用
T()
填充的。对于
int
,这是
0
。您可以亲自看到这一点:

#include <iostream>

using namespace std;

int main() {
    cout << int() << endl; // prints 0
}
#包括
使用名称空间std;
int main(){

学究般地说,这就是所谓的值初始化。@ildjarn确切地说,哪一部分被称为值初始化?@Cem默认值仍然是
0
@Seth:用一组空括号初始化一个类型。这个概念在§8.5中有定义;请参阅standardese。@Cem好吧,另一个答案几乎说明了一切。初始化一个值类型为T的对象表示:…(类类型)…否则,该对象为零初始化。此外,实现不使用
T\ux;
,而是插入
T\ux()
,这是初始化新对象的值。