您的位置:首页 > 编程语言 > C语言/C++

c++ primer(第五版)笔记 第三章(4)数组

2014-08-07 02:20 351 查看
#include<iostream>
#include<string>
#include<iterator>
#include<vector>

using std::cout;
using std::cin;
using std::endl;
using std::string;

void practice_3_28();
int practice_3_36(int *, int *, int *, int *);
void practice_3_43();
int main()
{
//数组的维度必须在编译时是已知的,意味着必须是一个常量表达式,必须指定数组的类型,不允许用 auto 推断
//使用列表初始化时,允许忽略数组的维度,编译器根据列表的数量计算维度
//如果指明了维度,初始值的数量不能超过维度,如果少于维度,剩余的元素执行默认初始化
//对于字符数组,可以用字符串字面值初始化,注意字面值的结尾处还有一个空字符
char a1[] = "hello"; //a1的维度为6
cout << sizeof(a1) << endl;
//不允许将数组的内容拷贝给其他数组做初始值,也不能用数组为其他数组赋值
//char a2[6] = a1;

//使用下标访问数组元素,下标类型为 size_t,机器无关的无符号类型,定义在 cstddef 的头文件里
//使用方法同 vector 和 string,注意越界

//用到数组名的地方,编译器会自动将其替换为一个指向数组首元素的指针
string nums[] = { "one", "two", "three" };
string *p = nums;	//数组名 nums 其实是 nums[0] 的指针
auto p2(nums);		//p2 是个 string *, 指向 “one”
*p2 = "four";
cout << nums[0] << endl;	//输出 four

//可以使用定义在 iterator 头文件中的 begin(), end() 函数获得数组的首元素指针,和尾元素的下一个指针
//还可以通过对数组最后一个元素的下一个不存在的元素进行取地址操作获得 off-the-end pointer
string *pBegin = begin(nums), *pEnd = end(nums);	//*pEnd = &nums[3];
while (pBegin != pEnd)
{
cout << *pBegin << " ";
++pBegin;
}
cout << endl;

//指向数组的指针进行的运算和迭代器相似,2个迭代器的差是一种定义在 cstddef 中的ptrdiff_t 类型,带符号
constexpr size_t sz = 3;
int ai[sz] = {0,1,2};
int *pAi = ai, *eAi = ai + sz;
while(pAi < eAi)
{
cout << *pAi << " ";
++pAi;
}
//不同于 vector 和 string,内建类型的索引,进行减操作不是一个无符号类型
pAi = &ai[1];
int k = pAi[-1]; 	//	ai[0]

cout << k << endl;
practice_3_28();
int ai1[] = { 0, 1, 2, 3 };
int ai2[] = { 0, 1, 2, 3};

practice_3_36(std::begin(ai1), std::end(ai1), std::begin(ai2), std::end(ai2));

//c风格字符串(c-style character string),c++ 继承 c 而来,字符串字面值,不是类型,只是一种约定的写法,存放在字符数组中并以空字符结束(null terminated, '\0')
//相关操作 cstring 头文件中,使用这些操作时,传入的参数必须指向以空字符结束的字符数组
//允许使用数组初始化一个 vector,反之不可以
std::vector<int> vi(std::begin(ai1), std::end(ai1));

practice_3_43();
return 0;
}

//哪些定义是非法的
void practice_3_27()
{
unsigned buff_size = 1024;
//int ia[buff_size];	//buff_size 不是常量
int ia1[4 * 7 - 14];	//正确
//int ia2[txt_size()];	//当 txt_size() 为 constexpr 时正确
//char st[11] = "fundamental";	//字符串字面值结尾还有个空字符
}

//元素的值是什么
string sa[10];	//string 类默认初始化为空
int ia3[10];	//函数体外, int 初始化为 0
void practice_3_28()
{
string sa1[10];	//string 类默认初始化为空
int ia4[10];	//函数体内, int 未定义的
cout << "-" << sa[0] << "-" << endl;
cout << ia3[0] << endl;
cout << "-" << sa1[0] << "-" << endl;
cout << ia4[0] << endl;

}

//创建维度 10 的 int 数组,每个元素就是下标值,然后拷贝给另一个数组
void practice_3_32()
{
int ai[10] = {};
int ai2[10] = {};

for (size_t i = 0; i != 10; ++i)
ai[i] = i;
for (size_t i = 0; i != 10; ++i)
ai2[i] = ai[i];
}

//比较2个数组是否相等
//pb1,pb2 分别为2个数组的首元素指针,pe1,pe2分别为尾元素的下一个地址
//返回值:1为相等,0为不等
int practice_3_36(int *pb1, int *pe1, int *pb2, int *pe2)
{
auto sz1 = pe1 - pb1;
auto sz2 = pe2 - pb2;
if (sz1 != sz2)
{
cout << "array1 not equal array2" << endl;
return 0;
}
while (pb1 < pe1)
{
if (*pb1 != *pb2)
{
cout << "array1 not equal array2" << endl;
return 0;
}
++pb1;
++pb2;
}
cout << "array1 equal array2" << endl;
return 1;
}

void practice_3_43()
{
//范围 for
int ai[2][3] = { { 1, 6, 9 }, { 3, 5, 8 } };
for ( int (&p)[3] : ai)
for (int &q : p)
cout << q << " ";
cout << endl;
//普通 for,下标
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 3; ++j)
cout << ai[i][j] << " ";
cout << endl;
//普通 for,end, begin
for (int(*p)[3] = std::begin(ai); p != std::end(ai); ++p)
for (int *q = std::begin(*p); q != std::end(*p); ++q)
cout << *q << " ";
cout << endl;
//普通 for,指针
for (int(*p)[3] = ai; p != ai + 2; ++p)
for (int *q = *p; q != *p + 3; ++q)
cout << *q << " ";
cout << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: