系统环境: windows 10 1703
编译环境:Visual studio 2017
3.30
数组 ia 中共有 10 个元素,其下标分别为从 0 到 9。当 ix == array_size 即 ix == 10 时,数组的下标超出范围,出现索引错误。
3.31
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::vector;
int main()
{
int iarr[10];
for (size_t i = 0; i != 10; ++i)
{
iarr[i] = i;
}
for (auto a : iarr)
{
cout << a << endl;
}
return 0;
}
3.32
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::vector;
int main()
{
//数组
int iarr[10];
int iarr2[10];
for (size_t i = 0; i != 10; ++i)
{
iarr[i] = i;
}
for (size_t i = 0; i != 10; ++i)
{
iarr2[i] = iarr[i];
}
//vector
vector<int> ivec(10);
for (vector<int>::size_type i = 0; i != 10; ++i)
{
ivec[i] = i;
}
vector<int> ivec2 = ivec;
return 0;
}
3.33
如果不初始化 scores,因为其定义在 main 函数作用域内,故其元素将不被初始化,处于 undefined 状态。对其进行加法操作时,无法预料其结果。
3.34
功能是将 p1 指向 p2 指向的元素。若 p1 或 p2 其中之一非法,则该程序非法。
3.35
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int iarr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p1 = iarr;
for (p1; p1 != iarr + 10; ++p1)
{
*p1 = 0;
}
for (auto a : iarr)
{
cout << a << endl;
}
return 0;
}
3.36
#include <iostream>
#include <iterator> // 为了使用数组的std::end()函数
using std::cout;
using std::endl;
// 数组
int main()
{
int iarr1[] = {1, 2, 3, 4, 5, 6, 8};
int iarr2[] = {1, 2, 3, 4, 5, 6, 7};
int *b1 = iarr1;
int *b2 = iarr2;
int *e1 = std::end(iarr1);
int *e2 = std::end(iarr2);
if ((e1 - b1) == (e2 - b2))
{
for (b1; b1 != e1; ++b1)
{
if (*b1 != *b2)
{
cout << "Not Equal!!!" << endl;
cout << "iarr1 " << *b1 << endl;
cout << "iarr2 " << *b2 << endl;
}
++b2;
}
}
else
{
cout << "Not Equal!!!" << endl;
}
return 0;
}
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
// vector
int main()
{
vector<int> ivec1(10, 2);
vector<int> ivec2(11, 3);
if (ivec1 != ivec2)
{
cout << "Not Euqal!!!" << endl;
}
return 0;
}