简体   繁体   English

C ++中数组上使用的等式和赋值运算符

[英]Equality & assignment operators used on arrays in C++

I was given a homework question that really confuses me. 我得到了一个让我困惑的作业问题。 The question is: 问题是:

In C++ the equality test == may be applied to arrays, but the assignment operator = cannot be applied to arrays. 在C ++中,等式测试==可以应用于数组,但赋值运算符=不能应用于数组。 Explain why. 解释为什么。

This confuses me, because my understanding is that the == operator would just compare the addresses of the first two elements (which if two arrays were in fact held in separate memory locations, of course would be different). 这让我感到困惑,因为我的理解是==运算符只会比较前两个元素的地址(如果两个数组实际上保存在不同的内存位置,当然会有所不同)。 And the = operator, when used like array1 = array2; =运算符一样,当像array1 = array2;一样使用时array1 = array2; would just cause array1 to point to the same memory location as array2 does. 只会导致array1指向与array2相同的内存位置。

What am I missing here? 我在这里错过了什么? It seems as though either operator can be used, but neither would produce the results typically intended by those operators. 似乎可以使用任一运算符,但两者都不会产生通常由这些运算符预期的结果。

my understanding is that the == operator would just compare the addresses of the first two elements 我的理解是==运算符只会比较前两个元素的地址

This is correct: if you compare two arrays using == , it will compare the addresses of the arrays, so it will only yield true if you compare an array with itself (or with a pointer to an element of the same type). 这是正确的:如果使用==比较两个数组,它将比较数组的地址,因此如果您将数组与自身(或指向同一类型元素的指针)进行比较,它将仅产生true See the description below for why. 请参阅以下说明了解原因。

the = operator, when used like array1 = array2; =运算符,当像array1 = array2一样使用时; would just cause array1 to point to the same memory location as array2 does. 只会导致array1指向与array2相同的内存位置。

This is not correct because an array is not a pointer . 这是不正确的,因为数组不是指针 array1 can't point to the same memory location as array2 because array1 isn't a pointer, it's an array of elements. array1不能指向与array2相同的内存位置,因为array1不是指针,它是一个元素数组。

An array is a sequence of elements. 数组是一系列元素。 In most contexts, the name of an array is implicitly converted to a pointer to its initial element. 在大多数上下文中,数组的名称被隐式转换为指向其初始元素的指针。 This is why you can do things like: 这就是为什么你可以这样做的原因:

void f(int*);

int data[10];
int* p = data; // this is the same as 'int* p = &data[0];'
f(data);       // this is the same as 'f(&data[0]);'

array1 = array2; won't work because arrays are not assignable (mostly for historical reasons; I've never heard a convincing technical reason why it isn't allowed: it was never allowed in C, and C has been around for decades. There's some discussion of this in the comments and answers to Why does C support memberwise assignment of arrays within structs but not generally? ). 因为数组不可分配(主要是出于历史原因;我从未听过一个令人信服的技术理由,为什么它不被允许):C从未被允许,而且C已经存在了几十年。这里有一些讨论这在为什么C支持在结构中成员分配数组而不是一般情况下的注释和答案 )。

The following program will not compile: 以下程序将无法编译:

int main() {
    int a[10], b[10];
    a = b;
}

For an "assignable" array, you can use the array container-like class found in Boost ( boost::array ), C++ TR1 ( std::tr1::array ), or C++0x ( std::array ). 对于“可赋值”数组,可以使用Boost( boost::array ),C ++ TR1( std::tr1::array )或C ++ 0x( std::array )中的类array容器类。 It is actually a class that contains an array; 它实际上是一个包含数组的类; it can be copied and it provides many of the benefits of the Standard Library containers plus the performance characteristics of an array and the ability to use its data as an array when you need to. 它可以被复制,它提供了标准库容器的许多好处,加上数组的性能特征以及在需要时将其数据用作数组的能力。

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

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