读书笔记:C++ Primer系列(9)—— 数组与指针(1)
2013-12-26 22:01 Keiven_LY 阅读(205) 评论(0) 编辑 收藏 举报题记:
数组与指针是我们常见到的两个名词,但是要想弄明白,弄得透彻也不是件容易的事!
- 与vector类似,数组也可以保存某种类型的一组对象,区别在于:数组的长度是固定的,一经创建,就不允许添加新的元素。
- 数组没有获取其容量大小的size操作,也不提供push_back操作在其中自动添加元素。
9.1 数组
- 数组是由类型名、标识符和维数组成的复合数据类型。类型名规定了存放在数组中的元素的类型,维数指定了数组中包含的元素个数。
- 数组的维数必须是值大于等于1的常量表达式(包含:整型字面值常量、枚举常量、用常量表达式初始化的整型const对象)。
9.1.1数组的定义
程序清单—01
1 int GetSize() 2 { 3 return 10; 4 } 5 int main() 6 { 7 int i=10; 8 int arr_1[i];//error: 应输入常量表达式 9 const int i_1=GetSize(); 10 int arr_2[i_1]; //error: 这里i_1虽然是整型字面值常量,但需要运行时才知道其值,所以不能作为数组的维数 11 int arr_3[GetSize()];//error: 这里也是需要运行时才知道值 12 const int i_2=10; 13 int arr_4[i_2];//ok 14 int arr_5[10];//ok 15 16 system("pause"); 17 return 0; 18 }
9.1.2 数组的初始化
如果没有显式提供元素初始值,则数组元素会像普通变量一样初始化,即:
- 在函数体外定义的内置型数组,其元素均初始化为0;
- 在函数体内定义的内置型数组,其元素无初始化;
- 不管数组在哪里定义,如果元素类型是类类型,则自动调用该类的默认构造函数进行初始化;如果该类没有默认构造函数,则必须为该元素提供显式初始化。
例如:
const unsigned array_size=3;
int arr[array_size]={0,1,2};
显示初始化时,也可以不指定维数,编译器可根据列出的元素个数来确定数组的长度,如:
int arr[]={0,1,2};
- 如果指定了数组维数,则初始化列表提供的元素个数不能超过维数值;
- 如果维数大于列出的元素个数,则只初始化前面的数组元素,剩下的其他元素,若是内置类型则初始化为0,若是类类型,则调用默认构造函数进行初始化。
字符数组初始化有两种方法,即
- 一种是用花括号括起来、逗号隔开的字符字面值进行初始化;
- 一种是用字符串字面值进行初始化。
注:使用字符串字面值进行初始化时,接吻会自动包含一个额外的空字符(null)。
例如:
char arr1[]={'C','h','i','n','a'};
char arr2[]={'C','h','i','n','a','\0'};
char arr3[]="China";
上面定义的三个数组中,arr1的维数为5,arr2和arr3的维数均为6,其中arr3的末尾会自动添加一个空字符(null)。
9.1.3 数组操作
程序清单—02
#include <iostream> #include <cstddef> using std::cin; using std::cout; using std::endl; int main() { const int array_size=10; int arr[array_size]; /*vector使用vector::size_type作为下标的类型,而数组下标的类型为size_t ,其包含在头文件cstddef中 */ for(size_t i=0;i!=array_size;++i) { arr[i]=i; cout<<arr[i]<<' '; } cout<<endl; system("pause"); return 0; }
9.2 课后习题练习
P100 习题4.7: 编写必要的代码将一个数组赋给另一个数组,然后把这段代码改用vector实现,考虑将一个vector赋给另一个vector
程序清单—03
//编写代码将一个数组赋给另一个数组 #include <iostream> using std::cin; using std::cout; using std::endl; int main() { const size_t array_size=10; int arr1[array_size]; int arr2[array_size]; for(size_t index=0;index!=array_size;++index) { arr1[index]=index; } cout<<"数组1的元素为:"; for(size_t index=0;index!=array_size;++index) { cout<<arr1[index]<<' '; } cout<<endl; for(size_t index=0;index!=array_size;++index) { arr2[index]=arr1[index]; } cout<<"数组2的元素为:"; for(size_t index=0;index!=array_size;++index) { cout<<arr2[index]<<' '; } cout<<endl; system("pause"); return 0; }
使用vector来实现,如下:
程序清单—04
#include <iostream> #include<vector> using std::cin; using std::cout; using std::endl; using std::vector; int main() { vector<int> vec1(10,1);//vec1初始化为10个1 vector<int> vec2(vec1); cout<<"vector2中的元素为:"; for(vector<int>:: size_type index=0;index!=vec1.size();++index) { cout<<vec2[index]<<' '; } cout<<endl; system("pause"); return 0; }
P100 习题4.8:编写程序判断两个数组是否相等,然后编写一段类似的程序比较两个vector
程序清单—05
#include <iostream>
using std::cin; using std::cout; using std::endl; int main() { const size_t array_size=5; int ia1[array_size],ia2[array_size]; size_t ix; cout<<"Enter "<<array_size<<" numbers for array1:"; for(ix=0;ix!=array_size;++ix) { cin>>ia1[ix]; } cout<<"Enter "<<array_size<<" numbers for array2:"; for(ix=0;ix!=array_size;++ix) { cin>>ia2[ix]; } for(ix=0;ix!=array_size;++ix) { if(ia1[ix]!=ia2[ix]) { cout<<"They are different arrays!"<<endl; system("pause"); return 0; } } cout<<"They are the same arrays!"<<endl; system("pause"); return 0; }
程序清单—06
#include <iostream> #include<vector> using std::cin; using std::cout; using std::endl; using std::vector; int main() { vector<int> vec1,vec2; int val; cout<<"Enter a vector -1 to end"<<endl; cin>>val; while(val!=-1){ vec1.push_back(val); cin>>val; } cout<<"Enter another vector -1 to end"<<endl; cin>>val; while(val!=-1){ vec2.push_back(val); cin>>val; } if(vec2.size()!=vec1.size()){ cout<<"Vec1 is not equal to Vec2!"<<endl; system("pause"); return 0; } else if(vec1.size()==0&&vec2.size()==0){ cout<<"Vec1 is equal to Vec2!"<<endl; system("pause"); return 0; } else if(vec1.size()!=0&&vec2.size()==0){ cout<<"Vec1 is not equal to Vec2!"<<endl; system("pause"); return 0; } else if(vec1.size()==0&&vec2.size()!=0){ cout<<"Vec1 is not equal to Vec2!"<<endl; system("pause"); return 0; } else { vector<int>::iterator iter1,iter2; iter1=vec1.begin(); iter2=vec2.begin(); for(;iter1!=vec1.end();iter1++,iter2++){ if(*iter1!=*iter2){ cout<<"Vec1 is not equal to Vec2!"<<endl; system("pause"); return 0; } } } cout<<"Vec1 is equal to Vec2!"<<endl; system("pause"); return 0; }