平等和放大器;在C数组使用赋值运算符++ [英] Equality & assignment operators used on arrays in C++

查看:130
本文介绍了平等和放大器;在C数组使用赋值运算符++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个家庭作业的问题,真正让我困惑。现在的问题是:


  

在C ++中等价测试==可能
  施加到阵列,但分配
  操作员=不能施加到
  阵列。解释为什么。


这混淆了我,因为我的理解是, == 运营商将只比较前两个元素(如果这两个数组实际上是在单独举行的地址存储单元,当然会有所不同)。和 = 运算符,如使用时数组1 =数组2; 只会导致ARRAY1指向同一个内存位置数组2一样。

我缺少的是在这里吗?它好像任一操作者都可以使用,但既不会产生通常由那些运营商预期的结果。


解决方案

  

我的理解是,==操作符将只是比较前两个元素的地址


这是正确的:如果你使用 == ,它会比较阵列的地址比较两个数组,所以它只会产生如果比较的阵列与本身(或具有指针到相同类型的元件)。请参阅下面的说明为什么。


  

=运算符,如数组1 =数组2使用时,只会导致ARRAY1指向相同的存储单元数组2一样。


这是不正确的,因为数组不是指针数组1 不能指向同一个内存位置数组2 ,因为数组1 不是一个指针,它的元件的阵列

数组是元素的序列。在大多数情况下,数组的名称隐式转换为指针,其最初的元素。这就是为什么你可以做这样的事情:

 无效F(INT *);int数据[10];
为int * p =数据; //这是一样的'廉政* P =放大器;数据[0];
F(数据); //这是一样的F(安培;数据[0]);'

ARRAY1 =数组2; 不会起作用,因为数组是不可转让(主要是由于历史原因,我从来没有听说过有说服力的技术原因,这是不允许的:这是从来没有使用C允许的,并且C已经存在了几十年有评价和答案Why的确结构内的数组的C ++支持按成员分配,但一般不?)。

下面的程序将不能编译:

  INT的main(){
    诠释一个[10],B [10];
    A = B;
}

对于分配数组,你可以使用升压(的boost ::阵列阵列容器类一样$ C>),C ++ TR1(的std :: tr1 ::阵列),或C ++ 0x中(的std ::阵列)。它实际上是一种包含阵列的类;它可以被复制,它提供了许多的标准库容器加一个数组,并使用其数据作为阵列的功能的性能特点所带来的好处,当您需要。

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. 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; would just cause array1 to point to the same memory location as array2 does.

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). See the description below for why.

the = operator, when used like array1 = array2; would just cause array1 to point to the same memory location as array2 does.

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.

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?).

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). 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.

这篇关于平等和放大器;在C数组使用赋值运算符++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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