考虑以下情况,您有两组数组 - 其中一组具有一些存储有值的整数,您可以将它们放入数组中并轻松计算数组中的值。但是,当您有一个包含空整数的数组时 - 它似乎无法将值“存储”到整数值 - 为什么?
这是一个例子:
int empty1;
int empty2;
int contain1 = 100;
int contain2 = 200;
int container [2] = { contain1, contain2 };
int empty_container [2] = { empty1, empty2 };
int i = 0;
while ( i != 2 ) {
empty_container[i] = i;
cout << endl << "empty_container[i]: " << empty_container[i] << endl
<< "empty1: " << empty1 << endl << "empty2: " << empty2 << endl;
cout << "i: " << i << endl;
cout << "container[i]: " << container[i] << endl;
cout << "contain1: " << contain1 << endl;
cout << "contain2: " << contain2 << endl;
i++;
}
输出:
empty_container[i]: 0
empty1: 4197341
empty2: 0
i: 0
container[i]: 100
contain1: 100
contain2: 200
empty_container[i]: 1
empty1: 4197341
empty2: 0
i: 1
container[i]: 200
contain1: 100
contain2: 200
请注意,empty1 突然变为“4197341”,而 empty2 为 0,这是怎么回事?
UPD(来自评论):