C++ C++;将整数数组分配给相同大小的空整数数组

C++ C++;将整数数组分配给相同大小的空整数数组,c++,arrays,variable-assignment,C++,Arrays,Variable Assignment,我非常熟悉Java,这是允许的。然而,它看起来不像C++。在尝试分配valuesToGrab=UpdateingValues;时,我得到一个“无效数组分配” //these are class attributes int updatingValues[361] = {0}; int valuesToGrab[361] = {0}; //this is part of a function that is causing an error. for (unsigned int i=0; i

我非常熟悉Java,这是允许的。然而,它看起来不像C++。在尝试分配valuesToGrab=UpdateingValues;时,我得到一个“无效数组分配”

//these are class attributes
int updatingValues[361] = {0};
int valuesToGrab[361] = {0};

//this is part of a function that is causing an error. 
for (unsigned int i=0; i < 10; i++) {

    //this fills values with 361 ints, and num_values gets set to 361. 
    sick_lms.GetSickScan(values,num_values);

    //values has 361 ints, but a size of 2882, so I copy all the ints to an array
    //of size 361 to "trim" the array.
    for(int z = 0; z < num_values; z++){
        updatingValues[z] = values[z];
    }

    //now I want to assign it to valuesToGrab (another program will be 
    //constantly grabbing this array, and it can't grab it while it's being
    //populated above or there will be issues
    valuesToGrab = updatingValues; // THROWING ERROR
}
//这些是类属性
int updateingvalues[361]={0};
int valuesToGrab[361]={0};
//这是导致错误的函数的一部分。
for(无符号整数i=0;i<10;i++){
//这将使用361整数填充值,num_值设置为361。
sick_lms.GetSickScan(值,num_值);
//值有361个整数,但大小为2882,因此我将所有整数复制到一个数组中
//尺寸为361,用于“修剪”阵列。
对于(int z=0;z
<>我不想重复更新值,并将它逐个添加到ValueStHoGo中,但如果必须的话,我可以用C++ C++?

谢谢,

< P> C++中的标准成语为

#include <algorithm>
...
std::copy(values, values+num_values, updatingValues);
I vector完成了数组所做的一切(包括C++11中的静态初始化),但有一个定义良好的接口。使用迭代器,size、empty、resize、push_back等等

编辑 还值得注意的是,您可以组合向量和数组

std::vector<int> vect(my_array, my_array+10);
//or
std::vector<int> another_vector;
...
another_vector.assign(my_array, my_array+10);//delayed population

在C++中,使用数组代替的习惯容器是:代码> STD::vector < /Cube >,使用“代码>向量<代码>或数组,可以使用<代码> STD::Copy[/]代码> <代码>标题,这是C++中任何类型的容器复制的首选方式。
std::vector<int> updatingValues, valuesToGrab;

// Ensure the vector has sufficient capacity to accept values.
updatingValues.resize(361);

// Copy values from the array into the vector.
std::copy(values, values + 361, updatingValues.begin());
//        Source begin & end;   Destination begin.

// Copy one vector to another.
valuesToGrab = updatingValues;
同样,对于数组,如果您想要更多的C风格,可以使用C标准库函数
memcpy()
,从


对于
memcpy()
(以及它的近亲
memmove()
),您必须注意要复制的元素的大小;如果您说的是
361
,而不是
361*sizeof(int),您将复制361字节,而不是361代码> int  

请记住,数组是作为C和C++中的指针实现的。

特别是,堆栈上的数组可以可视化为指向内存中具有为数组请求的容量的常量位置的指针。此内存位于堆栈上。当您尝试
valuesToGrab=updateingvalues
时,您可以将此视为尝试将
updateingvalues
的地址复制到变量
valuesToGrab
。这不是尝试深度复制,而您似乎正在尝试深度复制。但是,
valuesToGrab
指向内存中的一个恒定位置,无法更新。标准对此有点具体,明确禁止分配数组,这就是为什么您要获得特定的你看到的错误


您需要使用循环或类似于
std::copy
或C的
memcpy
的东西将值从一个数组复制到另一个数组。

首先,我认为这不会满足您的要求,因为
valuesToGrab=updateingvalues;
将覆盖外部循环的每个周期的
值tograb

假设您确实想这样做,并且不想更改为向量:

std::copy(updatingValues, updatingValues+361, valuesToGrab);
你可以像对待任何std::算法中的std::容器一样对待普通数组,指针被算作随机访问迭代器


重新思考你的设计,你不需要“修剪”,你可能不需要复制。

对于C风格数组或“修剪”数组似乎完全不必要。修剪然后复制,你真的真的应该学习C&C++中的内存管理。(另一个程序将//不断捕获此数组,并且在上面填充时//无法捕获它,否则将出现问题”强烈暗示需要通过互斥或关键部分进行进程同步。谢谢各位。我同意修剪是不必要的,我知道我必须在某个时候用C++来查看线程。我还听说C++中内存管理非常重要,所以我也会研究。数组不是指针。数组的名称是CONV。表达式中的指针不可插入。您应该澄清,仅当您希望从一个数组复制到另一个数组时,
memcpy
才适用(尽管如果您获得指向相关元素的指针,您可以使用它在POD向量之间进行复制)。好的,+1:)(也可以抵消IMHO不合理的-1)您可以使用vector::assign而不是copy。它将为您分配空间。这就是我要做的。我只希望valuesToGrab具有最新的完整扫描。如果我没有此数组,而是有一个获取更新值的方法,那么它可能会在重新填充时获取它,并返回半满数组。您说得对修剪。我想我可以忽略这一点。谢谢。
std::vector<int> updatingValues, valuesToGrab;

// Ensure the vector has sufficient capacity to accept values.
updatingValues.resize(361);

// Copy values from the array into the vector.
std::copy(values, values + 361, updatingValues.begin());
//        Source begin & end;   Destination begin.

// Copy one vector to another.
valuesToGrab = updatingValues;
std::copy(valuesToGrab, valuesToGrab + 361, updatingValues);
memcpy(valuesToGrab, updatingValues, 361 * sizeof(int));
//     Destination;  Source;         Number of bytes.
std::copy(updatingValues, updatingValues+361, valuesToGrab);