#include <array>

 

array是静态数组,在栈上,不可以变长

vector比array更常用

不需要变长,容量较小,用array

需要变长,容量较大,用vector

 

1 array

 

1 array新数组

 

//std::array<数组元素类型, 数组列数> 一维数组变量名

std::array<double, 4> dbnew1 = { 10.1,10.2,10.3,10.4 };//新数组

 

std::array<double, 4> dbnew2 = dbnew1;//新版数组可以实现整体赋值,适合于操作对象数组

 

 1 #include <iostream>
 2 #include <array>
 3 using namespace std;
 4 
 5 void main()
 6 {
 7     double db[4] = { 1.1,2.2,3.3,4.4 };//旧数组
 8 
 9     //std::array<数组元素类型, 数组元素个数> 一维数组变量名
10     std::array<double, 4> dbnew1 = { 10.1,10.2,10.3,10.4 };//新数组
11 
12     std::array<double, 4> dbnew2 = dbnew1;//新版数组可以实现整体赋值,适合于操作对象数组
13 
14     for (int i = 0; i < 4; i++)//打印
15     {
16         std::cout << db[i] << " ";
17     }
18     std::cout << std::endl;
19 
20     for (int i = 0; i < 4; i++)//打印
21     {
22         std::cout << dbnew1[i] << " ";
23     }
24     std::cout << std::endl;
25 
26     for (int i = 0; i < 4; i++)//打印
27     {
28         std::cout << dbnew2[i] << " ";
29     }
30 
31     system("pause");
32 }

 

使用C++风格数组不需要管理内存

array注意不要栈溢出

array适用于任何类型

 

array二维数组,三维数组

 

//std::array<数组元素类型, 数组列数> 一维数组变量名

//std::array<std::array<数组元素类型, 数组行数>, 数组列数> 二维数组变量名

 

 1 #include <iostream>
 2 #include <array>
 3 using namespace std;
 4 
 5 void main()
 6 {
 7     //std::array<数组元素类型, 数组列数> 一维数组变量名
 8     std::array<int, 5>myint1 = { 1,2,3,4,5 };//一维数组
 9     std::array<int, 5>myint2 = { 11,12,13,14,15 };//一维数组
10     std::array<int, 5>myint3 = { 21,22,23,24,25 };//一维数组
11 
12     //std::array<std::array<数组元素类型, 数组行数>, 数组列数> 二维数组变量名
13     std::array<std::array<int, 5>, 3>myint = { myint1,myint2,myint3 };//内嵌数组,二维数组
14     std::array<std::array<int, 5>, 3>myinta = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };//二维数组
15 
16     std::array<std::array<std::array<int, 5>, 3>, 4>myintb = { 0 };//三维数组
17 
18     for (int i = 0; i < 3; i++)//打印二维数组打印
19     {
20         for (int j = 0; j < 5; j++)
21         {
22             std::cout << " " << myint[i][j];
23         }
24         std::cout << std::endl;
25     }
26     std::cout << std::endl;
27 
28     for (int i = 0; i < 3; i++)//打印二维数组打印
29     {
30         for (int j = 0; j < 5; j++)
31         {
32             std::cout << " " << myinta[i][j];
33         }
34         std::cout << std::endl;
35     }
36     std::cout << std::endl;
37 
38     for (int i = 0; i < 4; i++)//打印三维数组
39     {
40         for (int j = 0; j < 3; j++)
41         {
42             for (int k = 0; k < 5; k++)
43             {
44                 std::cout << " " << myintb[i][j][k];
45             }
46             std::cout << std::endl;
47         }
48         std::cout << std::endl;
49     }
50 
51     system("pause");
52 }

 

迭代器循环遍历数组

从头到尾iterator

从尾到头reverse_iterator

 

 1 #include <iostream>
 2 #include <array>
 3 using namespace std;
 4 
 5 void main()
 6 {
 7     array<int, 5> myint = { 1,2,3,4,5 };
 8 
 9     array<int, 5>::iterator ibegin, iend;//迭代器
10     ibegin = myint.begin();//数据起始点
11     iend = myint.end();//结束
12 
13     for (; ibegin != iend; ibegin++)//从头到尾
14     {
15         cout << *ibegin << endl;
16     }
17 
18     array<int, 5>::reverse_iterator rbegin, rend;//迭代器
19     rbegin = myint.rbegin();//数据起始点
20     rend = myint.rend();//结束
21 
22     while (rbegin != rend)//从尾到头
23     {
24         cout << *rbegin << endl;
25         rbegin++;
26     }
27 
28     system("pause");
29 }

 

//当构造函数带有参数的情况下,如果要建立类的数组,这时候必须要用C++风格数组

 

 1 #include <iostream>
 2 #include <array>
 3 
 4 class classobj
 5 {
 6 public:
 7     int num;
 8     explicit classobj(int data)//构造函数需要参数
 9     {
10         this->num = data;
11         std::cout << "被构造" << num << std::endl;
12     }
13     ~classobj()
14     {
15         std::cout << "被销毁" << num << std::endl;
16     }
17 };
18 
19 void run()
20 {
21     classobj obj(0);//创建对象必须有合适的构造函数
22 
23     classobj *p = new classobj(5);//创建指针
24 
25     std::array <classobj, 2>myarray = { obj,*p };//当构造函数带有参数的情况下,如果要建立类的数组,这时候必须要用C++风格数组
26 }
27 
28 void main()
29 {
30     run();
31 
32     system("pause");
33 }

 

转载于:https://www.cnblogs.com/denggelin/p/5615276.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值