简体   繁体   English

数组之间的区别<int, size>名称和整数名称[大小]</int,>

[英]Difference between array<int, size> name and int name[size]

I found two ways to create an array.我找到了两种创建数组的方法。 What is the difference between them?它们之间有什么区别?

  1. array<int, 5> arr = {5, 8, 1, 3, 6};
  2. int arr[5] = {5, 8, 1, 3, 6};

I also found that I can use arr.size() in 1 , but I have to use sizeof(arr)/sizeof(*arr) in 2 .我还发现我可以在1中使用arr.size() ,但我必须在2中使用sizeof(arr)/sizeof(*arr)

TYPE NAME[SIZE] has always been the way to declare an array and C++ inherited that from C TYPE NAME[SIZE]一直是声明数组的方式,C++ 继承自 C

OTOH std::array is a new feature in C++11. OTOH std::array是 C++11 中的新功能。 It's a struct wrapping the old array inside.这是一个将旧数组包装在里面的结构。 It has a member named size() to get the number of elements of the internal array.它有一个名为size()的成员来获取内部数组的元素数。 A plain array doesn't have any methods so obviously you can't get the size that way一个普通的数组没有任何方法,所以很明显你不能那样得到大小

what is the using of pointers to find the length when you can just use sizeof()?当您可以使用 sizeof() 时,使用指针来查找长度是什么?

sizeof returns the size in bytes, not the number of elements. sizeof返回以字节为单位的大小,而不是元素的数量。 And you don't need to use pointers to get the length.而且您不需要使用指针来获取长度。 sizeof(arr)/sizeof(arr[0]) and sizeof(arr)/sizeof(int) are 2 common ways to get size, with the latter more prone to error when changing the type of the array sizeof(arr)/sizeof(arr[0])sizeof(arr)/sizeof(int)是两种常用的获取大小的方法,后者在更改数组类型时更容易出错

A plain array also decays to pointer so you can't get the size of it inside functions.普通数组也会衰减为指针,因此您无法在函数中获取它的大小。 You must get the size beforehand using sizeof and pass the size to the function explicitly.您必须事先使用sizeof获取大小并将大小显式传递给 function。 std::array has information about size so the size is known inside functions, but you'll need to write a template to deal with different array sizes which results in multiple implementations of the same function because each instantiation of a templated type is a different type. std::array具有有关大小的信息,因此在函数内部知道大小,但是您需要编写一个模板来处理不同的数组大小,这会导致相同 function 的多个实现,因为模板化类型的每个实例化都是不同的类型。 In this case passing a pointer and size of the array is the better solution在这种情况下,传递数组的指针和大小是更好的解决方案

 array<int, 5> arr = {5, 8, 1, 3, 6}

This needs to #include <array> .这需要#include <array> You're creating an object (of class array ) that accepts template arguments int and 5 .您正在创建一个接受模板 arguments int5的 object (属于 class array )。 By using this container, you can have the benefit of the built-in size() member function.通过使用此容器,您可以受益于内置的size()成员 function。

 int arr[5] = {5, 8, 1, 3, 6};

This is a simple C-style array declaration.这是一个简单的 C 风格数组声明。 This is not an object from any class, neither it needs any header to be declared.这不是来自任何 class 的 object,也不需要声明任何 header。 You need to make your user-defined size() function.您需要使用户定义的size() function。 Note that you're trying to get the size of a pointer to integer – sizeof(arr) / sizeof(*arr) that returns the size of the elements in bytes.请注意,您正在尝试获取指向 integer 的指针的大小 – sizeof(arr) / sizeof(*arr)以字节为单位返回元素的大小。 You need to use sizeof(arr) / sizeof(arr[0]) instead – so this returns the total number of elements.您需要改用sizeof(arr) / sizeof(arr[0]) - 所以这会返回元素的总数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM