如何在 C++ 中使用数组? [英] How do I use arrays in C++?

查看:58
本文介绍了如何在 C++ 中使用数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++ 从 C 继承了数组,它们几乎无处不在.C++ 提供了更易于使用且不易出错的抽象(std::vector 自 C++98 和 std::arrayC++11),因此对数组的需求不像在 C 中那样频繁出现.但是,当您阅读遗留代码或交互时使用 C 语言编写的库,您应该牢牢掌握数组的工作原理.

C++ inherited arrays from C where they are used virtually everywhere. C++ provides abstractions that are easier to use and less error-prone (std::vector<T> since C++98 and std::array<T, n> since C++11), so the need for arrays does not arise quite as often as it does in C. However, when you read legacy code or interact with a library written in C, you should have a firm grasp on how arrays work.

这个常见问题分为五个部分:

This FAQ is split into five parts:

  1. 类型级别的数组和访问元素
  2. 数组创建和初始化
  3. 赋值和参数传递
  4. 多维数组和指针数组
  5. 使用数组时的常见陷阱莉>

如果您觉得此常见问题解答中缺少某些重要内容,请写下答案并将其作为附加部分链接到此处.

If you feel something important is missing in this FAQ, write an answer and link it here as an additional part.

在下面的文本中,数组"指的是表示C 数组",而不是类模板 std::array.假定已具备 C 声明符语法的基本知识.请注意,如下所示手动使用 newdelete 在遇到异常时是极其危险的,但这就是 另一个常见问题解答.

In the following text, "array" means "C array", not the class template std::array. Basic knowledge of the C declarator syntax is assumed. Note that the manual usage of new and delete as demonstrated below is extremely dangerous in the face of exceptions, but that is the topic of another FAQ.

(注意:这是Stack Overflow 的 C++ 常见问题解答的条目.如果您想批评以这种形式提供常见问题解答的想法,然后 开始这一切的元数据发布将是这样做的地方.该问题的答案在 C++ 聊天室,FAQ 的想法首先从这里开始,因此您的答案很可能会被提出该想法的人阅读.)

(Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.)

推荐答案

类型级别的数组

数组类型表示为 T[n],其中 T元素类型n 是一个正的大小,数组中元素的数量.数组类型是元素类型和大小的乘积类型.如果这些成分中的一种或两种不同,您就会得到不同的类型:

Arrays on the type level

An array type is denoted as T[n] where T is the element type and n is a positive size, the number of elements in the array. The array type is a product type of the element type and the size. If one or both of those ingredients differ, you get a distinct type:

#include <type_traits>

static_assert(!std::is_same<int[8], float[8]>::value, "distinct element type");
static_assert(!std::is_same<int[8],   int[9]>::value, "distinct size");

注意大小是类型的一部分,即不同大小的数组类型是不兼容的类型,彼此之间绝对没有任何关系.sizeof(T[n]) 等价于 n * sizeof(T).

Note that the size is part of the type, that is, array types of different size are incompatible types that have absolutely nothing to do with each other. sizeof(T[n]) is equivalent to n * sizeof(T).

T[n]T[m] 之间唯一的联系"是这两种类型都可以隐式地转换T*,这个转换的结果是一个指向数组第一个元素的指针.也就是说,任何需要 T* 的地方,你都可以提供一个 T[n],编译器会默默地提供那个指针:

The only "connection" between T[n] and T[m] is that both types can implicitly be converted to T*, and the result of this conversion is a pointer to the first element of the array. That is, anywhere a T* is required, you can provide a T[n], and the compiler will silently provide that pointer:

                  +---+---+---+---+---+---+---+---+
the_actual_array: |   |   |   |   |   |   |   |   |   int[8]
                  +---+---+---+---+---+---+---+---+
                    ^
                    |
                    |
                    |
                    |  pointer_to_the_first_element   int*

这种转换被称为数组到指针的衰减",它是一个主要的混淆源.数组的大小在此过程中丢失,因为它不再是类型 (T*) 的一部分.优点:在类型级别忘记数组的大小允许指针指向任意大小的数组的第一个元素.缺点:给定一个指向数组第一个(或任何其他)元素的指针,无法检测该数组有多大或该指针相对于数组边界所指向的确切位置.指针非常愚蠢.

This conversion is known as "array-to-pointer decay", and it is a major source of confusion. The size of the array is lost in this process, since it is no longer part of the type (T*). Pro: Forgetting the size of an array on the type level allows a pointer to point to the first element of an array of any size. Con: Given a pointer to the first (or any other) element of an array, there is no way to detect how large that array is or where exactly the pointer points to relative to the bounds of the array. Pointers are extremely stupid.

只要认为数组的第一个元素有用,编译器就会默默地生成一个指向数组第一个元素的指针,也就是说,每当一个操作在数组上失败但在一个指针上成功时.这种从数组到指针的转换是微不足道的,因为结果指针 value 只是数组的地址.请注意,该指针存储为数组本身(或内存中的任何其他位置)的一部分.数组不是指针.

The compiler will silently generate a pointer to the first element of an array whenever it is deemed useful, that is, whenever an operation would fail on an array but succeed on a pointer. This conversion from array to pointer is trivial, since the resulting pointer value is simply the address of the array. Note that the pointer is not stored as part of the array itself (or anywhere else in memory). An array is not a pointer.

static_assert(!std::is_same<int[8], int*>::value, "an array is not a pointer");

数组不会衰减为指向其第一个元素的指针的一个重要上下文是对它应用了 & 运算符.在这种情况下,& 运算符产生一个指向 整个 数组的指针,而不仅仅是指向其第一个元素的指针.尽管在那种情况下(地址)是相同的,但是指向数组第一个元素的指针和指向整个数组的指针是完全不同的类型:

One important context in which an array does not decay into a pointer to its first element is when the & operator is applied to it. In that case, the & operator yields a pointer to the entire array, not just a pointer to its first element. Although in that case the values (the addresses) are the same, a pointer to the first element of an array and a pointer to the entire array are completely distinct types:

static_assert(!std::is_same<int*, int(*)[8]>::value, "distinct element type");

以下 ASCII 艺术解释了这种区别:

The following ASCII art explains this distinction:

      +-----------------------------------+
      | +---+---+---+---+---+---+---+---+ |
+---> | |   |   |   |   |   |   |   |   | | int[8]
|     | +---+---+---+---+---+---+---+---+ |
|     +---^-------------------------------+
|         |
|         |
|         |
|         |  pointer_to_the_first_element   int*
|
|  pointer_to_the_entire_array              int(*)[8]

注意第一个元素的指针是如何只指向一个整数(描绘成一个小盒子),而指向整个数组的指针指向一个由 8 个整数组成的数组(描绘成一个大盒子).

Note how the pointer to the first element only points to a single integer (depicted as a small box), whereas the pointer to the entire array points to an array of 8 integers (depicted as a large box).

同样的情况出现在课堂上,而且可能更加明显.指向对象的指针和指向其第一个数据成员的指针具有相同的(相同的地址),但它们是完全不同的类型.

The same situation arises in classes and is maybe more obvious. A pointer to an object and a pointer to its first data member have the same value (the same address), yet they are completely distinct types.

如果您不熟悉 C 声明符语法,int(*)[8] 类型中的括号是必不可少的:

If you are unfamiliar with the C declarator syntax, the parenthesis in the type int(*)[8] are essential:

  • int(*)[8] 是一个指向 8 个整数数组的指针.
  • int*[8] 是一个包含 8 个指针的数组,每个元素都是 int* 类型.
  • int(*)[8] is a pointer to an array of 8 integers.
  • int*[8] is an array of 8 pointers, each element of type int*.

C++ 提供了两种语法变体来访问数组的各个元素.它们都不优于另一个,您应该熟悉两者.

C++ provides two syntactic variations to access individual elements of an array. Neither of them is superior to the other, and you should familiarize yourself with both.

给定一个指向数组第一个元素的指针 p,表达式 p+i 产生一个指向数组第 i 个元素的指针.之后通过取消引用该指针,可以访问单个元素:

Given a pointer p to the first element of an array, the expression p+i yields a pointer to the i-th element of the array. By dereferencing that pointer afterwards, one can access individual elements:

std::cout << *(x+3) << ", " << *(x+7) << std::endl;

如果x表示一个数组,那么数组到指针的衰减就会开始,因为添加一个数组和一个整数是没有意义的(没有加号操作数组),但添加一个指针和一个整数是有意义的:

If x denotes an array, then array-to-pointer decay will kick in, because adding an array and an integer is meaningless (there is no plus operation on arrays), but adding a pointer and an integer makes sense:

   +---+---+---+---+---+---+---+---+
x: |   |   |   |   |   |   |   |   |   int[8]
   +---+---+---+---+---+---+---+---+
     ^           ^               ^
     |           |               |
     |           |               |
     |           |               |
x+0  |      x+3  |          x+7  |     int*

(注意隐式生成的指针是没有名字的,所以我写了x+0来标识)

(Note that the implicitly generated pointer has no name, so I wrote x+0 in order to identify it.)

另一方面,如果 x 表示指向数组第一个(或任何其他)元素的指针,则数组到指针的衰减不是必须的,因为要在其上添加 i 的指针已经存在:

If, on the other hand, x denotes a pointer to the first (or any other) element of an array, then array-to-pointer decay is not necessary, because the pointer on which i is going to be added already exists:

   +---+---+---+---+---+---+---+---+
   |   |   |   |   |   |   |   |   |   int[8]
   +---+---+---+---+---+---+---+---+
     ^           ^               ^
     |           |               |
     |           |               |
   +-|-+         |               |
x: | | |    x+3  |          x+7  |     int*
   +---+

请注意,在描述的情况下,x 是一个指针变量(可以通过 x 旁边的小框识别),但它可以也可以是返回指针的函数的结果(或任何其他 T* 类型的表达式).

Note that in the depicted case, x is a pointer variable (discernible by the small box next to x), but it could just as well be the result of a function returning a pointer (or any other expression of type T*).

由于语法*(x+i)有点笨拙,C++提供了替代语法x[i]:

Since the syntax *(x+i) is a bit clumsy, C++ provides the alternative syntax x[i]:

std::cout << x[3] << ", " << x[7] << std::endl;

由于加法是可交换的,下面的代码完全一样:

Due to the fact that addition is commutative, the following code does exactly the same:

std::cout << 3[x] << ", " << 7[x] << std::endl;

索引运算符的定义导致以下有趣的等价:

The definition of the indexing operator leads to the following interesting equivalence:

&x[i]  ==  &*(x+i)  ==  x+i

然而,&x[0] 通常等同于 x.前者是指针,后者是数组.只有当上下文触发数组到指针衰减时,x&x[0] 可以互换使用.例如:

However, &x[0] is generally not equivalent to x. The former is a pointer, the latter an array. Only when the context triggers array-to-pointer decay can x and &x[0] be used interchangeably. For example:

T* p = &array[0];  // rewritten as &*(array+0), decay happens due to the addition
T* q = array;      // decay happens due to the assignment

在第一行,编译器检测到从一个指针到一个指针的赋值,这很容易成功.在第二行,它检测从 array 到指针的赋值.由于这是毫无意义的(但指针指向指针赋值是有意义的),数组到指针的衰减照常开始.

On the first line, the compiler detects an assignment from a pointer to a pointer, which trivially succeeds. On the second line, it detects an assignment from an array to a pointer. Since this is meaningless (but pointer to pointer assignment makes sense), array-to-pointer decay kicks in as usual.

T[n] 类型的数组有 n 个元素,索引从 0n-1;没有元素 n.然而,为了支持半开范围(其中开头是包含,结尾是不包含),C++ 允许计算指向(不存在的)的指针第 n 个元素,但取消引用该指针是非法的:

An array of type T[n] has n elements, indexed from 0 to n-1; there is no element n. And yet, to support half-open ranges (where the beginning is inclusive and the end is exclusive), C++ allows the computation of a pointer to the (non-existent) n-th element, but it is illegal to dereference that pointer:

   +---+---+---+---+---+---+---+---+....
x: |   |   |   |   |   |   |   |   |   .   int[8]
   +---+---+---+---+---+---+---+---+....
     ^                               ^
     |                               |
     |                               |
     |                               |
x+0  |                          x+8  |     int*

例如,如果您想对数组进行排序,以下两种方法同样适用:

For example, if you want to sort an array, both of the following would work equally well:

std::sort(x + 0, x + n);
std::sort(&x[0], &x[0] + n);

请注意,提供 &x[n] 作为第二个参数是非法的,因为这等效于 &*(x+n),并且子表达式 *(x+n) 在技术上调用了 C++ 中的未定义的行为(但不是在 C99 中)).

Note that it is illegal to provide &x[n] as the second argument since this is equivalent to &*(x+n), and the sub-expression *(x+n) technically invokes undefined behavior in C++ (but not in C99).

另请注意,您可以简单地提供 x 作为第一个参数.这对我来说有点太简洁了,而且它也使编译器的模板参数推导有点困难,因为在这种情况下,第一个参数是一个数组,而第二个参数是一个指针.(再次,数组到指针的衰减开始了.)

Also note that you could simply provide x as the first argument. That is a little too terse for my taste, and it also makes template argument deduction a bit harder for the compiler, because in that case the first argument is an array but the second argument is a pointer. (Again, array-to-pointer decay kicks in.)

这篇关于如何在 C++ 中使用数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆