c++ array模板类使用

目录

1、array模板类的定义

(1)array模板类的声明

(2)容器属性

(3)array模板类的说明

(4)array模板类头文件

2、array模板类的使用

(1)Iterators

(2)Capacity

(3)Element access

(4)Modifiers

(5)Compare

(6)Other


本章是对c++ array模板类的知识归纳,讲述了c++中array模板类的使用,不涉及原理方面的内容。

c++中的数组类型是继承了c语言的特性,在使用数组的时候要注意数组越界操作问题。为了更安全的对数组进行操作,c++提出了数组模板类array。


1、array模板类的定义

(1)array模板类的声明

template <class T,size_t N> class array;

数组类是固定大小的序列容器,它们包含以严格线性序列排序的特定数量的元素。数组类具有固定大小,并且不通过分配器管理其元素的分配,它们是封装固定大小元素数组的聚合类型。

(2)容器属性

  • 序列容器中的元素按严格的线性顺序排序。各个元素按其顺序访问它们的位置。
  • 元素存储在连续的存储器位置,允许对元素进行恒定时间随机访问。可以偏移元素的指针以访问其他元素。
  • 容器使用隐式构造函数和析构函数静态分配所需的空间。它的大小是编译时常量。没有内存或时间开销。

(3)array模板类的说明

array模板类中T为包含元素的类型(std::array::value_type),N为元素个数。

(4)array模板类头文件

使用array模板类之前需要包含#include <array>头文件!


2、array模板类的使用

(1)Iterators

Iterators迭代器的作用是遍历array数组类中的元素。可以通过begin/end()rbegin/rend()cbegin/cend()crbegin/crend()等函数进行访问。

beginReturn iterator to beginning
endReturn iterator to end
rbeginReturn reverse iterator to reverse beginning
rendReturn reverse iterator to reverse end
cbeginReturn const_iterator to beginning
cendReturn const_iterator to end
crbeginReturn const_reverse_iterator to reverse beginning
crendReturn const_reverse_iterator to reverse end

参考代码如下所示:

 
  1. /*****************************************************

  2. Copyright (C) 2018. All rights reserved.

  3. File name : array.cpp

  4. Version : v1.0

  5. Author : zhengqijun

  6. Date : 2018-08-10

  7. Function List :

  8. Description : array container.

  9. ******************************************************/

  10.  
  11. #include <iostream>

  12. #include <array>

  13.  
  14. int main(void) {

  15. std::array<int, 5> arr = {1, 2, 3, 4, 5};

  16.  
  17. std::cout << "array values: ";

  18. for (auto it = arr.begin(); it != arr.end(); ++it) {

  19. std::cout << *it << " ";

  20. }

  21. std::cout << std::endl;

  22.  
  23. return 0;

  24. }

运行结果如下所示:

array values: 1 2 3 4 5

(2)Capacity

array数组容器的大小是固定的。可以通过sizeof()size()max_size()empty()等函数进行检测。

sizeReturn size
max_sizeReturn maximum size
emptyTest whether list is empty

测试array数组容器大小的参考代码如下所示:

 
  1. /*****************************************************

  2. Copyright (C) 2018. All rights reserved.

  3. File name : array.cpp

  4. Version : v1.0

  5. Author : zhengqijun

  6. Date : 2018-08-10

  7. Function List :

  8. Description : array container.

  9. ******************************************************/

  10.  
  11. #include <iostream>

  12. #include <array>

  13.  
  14. int main(void) {

  15. std::array<int, 5> arr = {1, 2, 3, 4, 5};

  16.  
  17. std::cout << "sizeof(array) = " << sizeof(arr) << std::endl;

  18. std::cout << "size of array = " << arr.size() << std::endl;

  19. std::cout << "max_size of array = " << arr.max_size() << std::endl;

  20.  
  21. if (arr.empty()) {

  22. std::cout << "array is empty!" << std::endl;

  23. } else {

  24. std::cout << "array is not empty!" << std::endl;

  25. }

  26.  
  27. return 0;

  28. }

运行结果如下所示:

 
  1. sizeof(array) = 20

  2. size of array = 5

  3. max_size of array = 5

  4. array is not empty!

(3)Element access

可以通过下标[ ]at()front()back()data()等函数访问array容器内的元素。

operator[ ]Access element
atAccess element 
frontAccess first element
backAccess last element
dataGet pointer to first data

参考代码如下:

 
  1. /*****************************************************

  2. Copyright (C) 2018. All rights reserved.

  3. File name : array.cpp

  4. Version : v1.0

  5. Author : zhengqijun

  6. Date : 2018-08-10

  7. Function List :

  8. Description : array container.

  9. ******************************************************/

  10.  
  11. #include <iostream>

  12. #include <array>

  13.  
  14. int main(void) {

  15. std::array<int, 5> arr = {1, 2, 3, 4, 5};

  16.  
  17. std::cout << "array[0] = " << arr[0] << std::endl;

  18. std::cout << "array.at(4) = " << arr.at(4) << std::endl;

  19. std::cout << "array.front() = " << arr.front() << std::endl;

  20. std::cout << "array.back() = " << arr.back() << std::endl;

  21. std::cout << "&array: " << arr.data() << " = " << &arr << std::endl;

  22.  
  23. return 0;

  24. }

运行结果如下所示:

 
  1. array[0] = 1

  2. array.at(4) = 5

  3. array.front() = 1

  4. array.back() = 5

  5. &array: 0x7ffd22df6e50 = 0x7ffd22df6e50

(4)Modifiers

可以使用fill()swap()等函数对array容器整体进行操作。

fillFill array with value
swapSwap content

参考代码如下所示:

 
  1. /*****************************************************

  2. Copyright (C) 2018. All rights reserved.

  3. File name : array.cpp

  4. Version : v1.0

  5. Author : zhengqijun

  6. Date : 2018-08-10

  7. Function List :

  8. Description : array container.

  9. ******************************************************/

  10.  
  11. #include <iostream>

  12. #include <array>

  13.  
  14. int main(void) {

  15. std::array<int, 5> arr;

  16.  
  17. arr.fill(5); // fill

  18.  
  19. std::cout << "array values: ";

  20. for (auto i : arr) {

  21. std::cout << i << " ";

  22. }

  23. std::cout << std::endl;

  24.  
  25. std::array<int, 3> first = {1, 2, 3};

  26. std::array<int, 3> second = {6, 5, 4};

  27.  
  28. std::cout << "first array values: ";

  29. for (auto it = first.begin(); it != first.end(); ++it) {

  30. std::cout << *it << " ";

  31. }

  32. std::cout << std::endl;

  33.  
  34. std::cout << "second array values: ";

  35. for (auto it = second.begin(); it != second.end(); ++it) {

  36. std::cout << *it << " ";

  37. }

  38. std::cout << std::endl;

  39.  
  40. first.swap(second); // swap

  41.  
  42. std::cout << "swap array success!" << std::endl;

  43.  
  44. std::cout << "first array values: ";

  45. for (auto it = first.begin(); it != first.end(); ++it) {

  46. std::cout << *it << " ";

  47. }

  48. std::cout << std::endl;

  49.  
  50. std::cout << "second array values: ";

  51. for (auto it = second.begin(); it != second.end(); ++it) {

  52. std::cout << *it << " ";

  53. }

  54. std::cout << std::endl;

  55.  
  56. return 0;

  57. }

运行结果如下所示:

 
  1. array values: 5 5 5 5 5

  2. first array values: 1 2 3

  3. second array values: 6 5 4

  4. swap array success!

  5. first array values: 6 5 4

  6. second array values: 1 2 3

(5)Compare

还可以使用>  <  ==等符号对两个array数组容器进行比较。

 
  1. /*****************************************************

  2. Copyright (C) 2018. All rights reserved.

  3. File name : array.cpp

  4. Version : v1.0

  5. Author : zhengqijun

  6. Date : 2018-08-10

  7. Function List :

  8. Description : array container.

  9. ******************************************************/

  10.  
  11. #include <iostream>

  12. #include <array>

  13.  
  14. int main(void) {

  15. std::array<int,5> a = {10, 20, 30, 40, 50};

  16. std::array<int,5> b = {10, 20, 30, 40, 50};

  17. std::array<int,5> c = {50, 40, 30, 20, 10};

  18.  
  19. if (a == b) {

  20. std::cout << "a == b" << std::endl;

  21. } else {

  22. std::cout << "a != b" << std::endl;

  23. }

  24.  
  25. if (a == c) {

  26. std::cout << "a == c" << std::endl;

  27. } else {

  28. std::cout << "a != c" << std::endl;

  29. }

  30.  
  31. if (a < c) {

  32. std::cout << "a < c" << std::endl;

  33. } else {

  34. std::cout << "a >= c" << std::endl;

  35. }

  36.  
  37. return 0;

  38. }

  39.  

运行结果如下所示:

 
  1. a == b

  2. a != c

  3. a < c

(6)Other

c++重载了get()函数来访问数组容器中的元素,为了和元组相似,还重载了tuple_sizetuple_element类型。

get( array)Get element (tuple interface)
tuple_element<array>Tuple element type for array
tuple_size<array>Tuple size traits for array

参考代码如下所示:

 
  1. /*****************************************************

  2. Copyright (C) 2018. All rights reserved.

  3. File name : array.cpp

  4. Version : v1.0

  5. Author : zhengqijun

  6. Date : 2018-08-10

  7. Function List :

  8. Description : array container.

  9. ******************************************************/

  10.  
  11. #include <iostream>

  12. #include <array>

  13. #include <tuple>

  14.  
  15. int main(void) {

  16. std::array<int,3> myarray = {10, 20, 30};

  17. std::tuple<int, int, int> mytuple (10, 20, 30);

  18.  
  19. std::tuple_element<0, decltype(myarray)>::type myelement; // int myelement

  20.  
  21. myelement = std::get<2>(myarray);

  22. std::get<2>(myarray) = std::get<0>(myarray);

  23. std::get<0>(myarray) = myelement;

  24.  
  25. std::cout << "first element in myarray: " << std::get<0>(myarray) << std::endl;

  26. std::cout << "first element in mytuple: " << std::get<0>(mytuple) << std::endl;

  27.  
  28. return 0;

  29. }

运行结果如下所示:

  1. first element in myarray: 30

  2. first element in mytuple: 10


建议:多使用array数组容器代替c类型数组,使操作数组元素更加安全!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值