C++(4)数组

数组


序言:

     尽管现代C++仍然支持数组与指针类型,但是,现代的C++程序应尽量使用vector和迭代器类型。设计良好的程序只有在强调速度时才在类的实现内部使用数组和指针!

    即:只有当性能测试表明使用vector无法达到必要的速度时,才使用数组!

正文:


1、数组定义中的类型可以是内置类型或类类型;除了引用之外,数组元素的类型可以是任意的复合类型。没有所有元素都是引用的数组*^_^*



2、非const变量以及直到运行阶段才知道其值的const对象不能用于指定数组的维数!

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. const int n = get_size();  
  2. int arr[n];         //ERROR  
  3.   
  4. int arr[get_size()];        //ERROR  
  5.   
  6. int arr_size = 110;  
  7. int arr[arr_size];      //EROR  

3、显式初始化数组元素

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. const unsigned array_size = 3;  
  2. int ia[array_size] = {0,1,2};  
  3.   
  4. int ia[] = {0,1,2};  

4、没有显示提供元素初值,则数组会像普通变量一样初始化

    1)在函数体外定义的内置元素数组,其元素均初始化为0;

    2)在函数体内定义的内置元素数组,不进行初始化!

    3)如果是类类型,则自动调用默认构造函数!



5、如果只是初始化了部分数组元素,那么剩下的元素,如果是内置类型则初始化为0,若是类类型,则调用他的默认构造函数。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. const int arr_size = 5;  
  2. int arr[arr_size] = {1,2};  
  3. for (int i = 0;i < arr_size; ++i)  
  4. {  
  5.     cout << arr[i] << ' ';  
  6. }  
  7. cout << endl;  
  8.   
  9. string arr2[arr_size] = {"Hello","World"};  
  10. for (int i = 0;i < arr_size; ++i)  
  11. {  
  12.     cout << arr2[i] << endl;  
  13. }  

6、特殊的字符数组!

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. char ca1[] = {'C','+','+'};     //没有结束符,size = 3,但是用strlen测试不出正确的结果!  
  2. char ca2[] = "C++";         //自动添加了NULL结束符,size = 4  
  3. char ca3[] = {'C','+','+','\0'};        //显式添加了NULL结束符  
  4. char ca4[6] = "Daniel";         //编译时错误!  

7、不允许数组直接复制或赋值!

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //P98 习题4.3 下列语句是否正确  
  2.     vector<int> ivec = {0,1,2,3,4};       //ERROR  
  3.   
  4.     int ia1[7] = {0,1,2,3,4,5,6};  
  5.     int ia2 = ia1;              //ERROR  

8、数组下标的正确类型是size_t

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //示例1  
  2. int main()  
  3. {  
  4.     const size_t arr_size = 10;  
  5.     int ia[arr_size];  
  6.     for (size_t i = 0;i < arr_size; ++i)  
  7.     {  
  8.         ia[i] = i + 1;  
  9.     }  
  10.     return 0;  
  11. }  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //示例2  
  2. int main()  
  3. {  
  4.     const size_t arr_size = 7;  
  5.     int arrSor[] = {0,1,2,3,4,5,6};  
  6.     int arrDes[arr_size];  
  7.   
  8.     for (size_t index = 0;index < arr_size; ++index)  
  9.     {  
  10.         arrDes[index] = arrSor[index];  
  11.     }  
  12.     return 0;  
  13. }  

9、一旦引用了越出数组或其他类似数据结构的边界的元素时,就会导致缓冲区溢出错误!

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //P100 习题4.7(2)  
  2. int main()  
  3. {  
  4.     vector<int> ivec1;  
  5.     for (int i = 0;i != 10; ++i)  
  6.     {  
  7.         ivec1.push_back(i);  
  8.     }  
  9.   
  10.     vector<int> ivec2(ivec1);  
  11.     for (vector<int>::iterator iter = ivec2.begin(); iter != ivec2.end(); ++iter)  
  12.     {  
  13.         cout << *iter << endl;  
  14.     }  
  15. }  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //习题4.8(1)  
  2. int main()  
  3. {  
  4.     const int arr_size = 7;  
  5.     int arr1[arr_size] = {1,2,3};  
  6.     int arr2[arr_size] = {4,5,6};  
  7.   
  8.     for (int i = 0;i != arr_size; ++i)  
  9.     {  
  10.         if (arr1[i] != arr2[i])  
  11.         {  
  12.             cout << "arr1 is not equal to arr2" << endl;  
  13.             break;  
  14.         }  
  15.     }  
  16.     return 0;  
  17. }  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //(2)  
  2. int main()  
  3. {  
  4.     vector<int> ivec1;  
  5.     vector<int> ivec2;  
  6.   
  7.     int value;  
  8.     while (cin >> value)  
  9.     {  
  10.         ivec1.push_back(value);  
  11.     }  
  12.     while (cin >> value)  
  13.     {  
  14.         ivec2.push_back(value);  
  15.     }  
  16.   
  17.     if (ivec1 == ivec2)  
  18.     {  
  19.         cout << "ivec1 is equal to ivec2" << endl;  
  20.     }  
  21.     else  
  22.     {  
  23.         cout << "ivec1 is not equal to ivec2" << endl;  
  24.     }  
  25.     return 0;  
  26. }  

本文借鉴:http://blog.csdn.net/zjf280441589/article/details/23021813

转载于:https://my.oschina.net/sfsimon/blog/659291

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值