简体   繁体   English

将C ++数组转换为向量

[英]cast an C++ array to a vector

Here is the code I found on Leetcode . 这是我在Leetcode上找到的代码。 However I cannot make sense of the following two lines, especially *(&a + 1) . 但是我无法理解以下两行,尤其是*(&a + 1) And the results show a copy of the array a . 结果显示了数组a的副本。 Could anyone give some explanation to this? 任何人都可以给出一些解释吗? Thanks! 谢谢!

    int a[5] = {0, 1, 2, 3, 4};
    vector<int> v4(a, *(&a + 1));

The examples that you normally face when constructing a vector from an array usually looks like the following: 从数组构造向量时通常遇到的示例通常如下所示:

int a[5] = {...};
vector<int> v4(a, a + 5 );
// or vector<int>(a, a+sizeof(a)/int); // automatically calculate num elems inside a

The example above simply shows you want to construct a vector using all the elements inside array "a". 上面的例子只是表明你想用数组“a”中的所有元素构造一个向量。 "a" in this example is just the address to the starting element of the array, then you add 5 to get the address of the last element. 此示例中的“a”只是数组起始元素的地址,然后添加5以获取最后一个元素的地址。 A more intuitive way to write this would be: 更直观的方式是:

vector<int> v4(begin(a), end(a));

Unless of course you don't want all the elements of a in v4 除非你当然不想要v4中的所有元素

Now the example you have given us is the shorthand of the first example where you don't need to explicitly state the size of the array. 现在您给出的示例是第一个示例的简写,您不需要显式声明数组的大小。 I'm assuming you are just confused with the second argument of the vector constructor. 我假设你只是与向量构造函数的第二个参数混淆。 The second argument just returns the address of the last element of array "a". 第二个参数只返回数组“a”的最后一个元素的地址。 But how? 但是怎么样?

Well let's break it down: &a returns the address to the "a[]". 好吧,让我们分解它:&a将地址返回到“a []”。 Essentially a pointer to the array "a". 本质上是指向数组“a”的指针。 Now if you add one to this address, you will get a pointer to the address "a[0]" + sizeof a[] which will point to the last element of address a. 现在,如果你向这个地址添加一个,你将得到一个指向地址“a [0]”+ sizeof a []的指针,它将指向地址a的最后一个元素。 Compare this to the first example above. 将此与上面的第一个示例进行比较。 You had to add 5, why is this not the case here? 你必须加5,为什么这不是这里的情况? Well you are working with different units. 那么你正在与不同的单位合作。 &a points to the starting address of a[] rather than the address of the first element of the array. &a指向[]的起始地址,而不是数组的第一个元素的地址。 So you are essentially moving the address in units of a[5] rather than units of int. 所以你基本上以[5]为单位而不是int单位移动地址。

Then finally you have the dereference operator "*" to dereference the pointer and get the address of array[] for the last element. 然后最后你有取消引用运算符“*”取消引用指针并获取最后一个元素的array []的地址。

a is type a[] and &a is type *a[] so the dereference is needed to make it the same type otherwise you get a compiler error. a是类型a []和&a是类型* a []因此需要取消引用以使其成为相同类型,否则会出现编译器错误。

TLDR: You are getting the address for the last element using different methods. TLDR:您使用不同的方法获取最后一个元素的地址。 The +1 operator behaves relative to the type you are dealing with. +1运算符的行为与您正在处理的类型相关。 a + 1 is the same as starting address of "a" + sizeof 1 integer. a + 1与起始地址“a”+ sizeof 1整数相同。 &a + 1 is the same as starting address of a[] + size of a[]. &a + 1与[]的[] +大小的起始地址相同。

You're getting confused by array decay. 您对阵列衰减感到困惑。 a is the array "as a whole". a是“作为一个整体”的数组。 It decays into a pointer pointing to the first element in MOST contexts, but the operand of unary-& is one of the few that it does not. 它衰减成一个指向MOST上下文中第一个元素的指针,但是unary-&的操作数是它没有的少数操作数之一。 So &a gives you the address of the array as a whole, not the address of the first element. 所以&a给出了整个数组的地址,而不是第一个元素的地址。 These are the same place, but have different types ( int (*)[5] vs int * ) and that different type means that pointer arithmetic is different. 这些是相同的地方,但有不同的类型( int (*)[5] vs int * ),不同的类型意味着指针算法不同。

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

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