[英]C++ assign array of ints to an empty array of ints same size
我对Java非常熟悉,这是允许的。 然而,看起来它不适用于C ++。 我在尝试分配valuesToGrab = updatingValues;时收到“无效的数组赋值”。
//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
}
我不想迭代更新VALal并将其逐个添加到valuesToGrab,但如果我必须,我会。 有没有办法用C ++在一个函数中分配它?
谢谢,
用C ++复制的标准习惯用法是
#include <algorithm>
...
std::copy(values, values+num_values, updatingValues);
确保updatingValues
足够大,否则你将会出现超支,并且会发生不好的事情。
那就是说在C ++中我们通常使用std :: vector来完成这类任务。
#include <vector>
...
std::vector<int> updatingValues=values; //calls vectors copy constructor
我向量执行数组所做的一切(包括C ++ 11中的静态初始化),但是有一个很好的定义接口。 with iterators,size,empty,resize,push_back等等。
http://en.cppreference.com/w/cpp/algorithm/copy
http://en.cppreference.com/w/cpp/container/vector
编辑值得注意的是,您可以组合矢量和数组。
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
反之亦然
std::copy(vect.begin(), vect.end(), my_array); //copy vector into array.
在C ++中,用于代替数组的惯用容器是std::vector
。 使用vector
或数组,您可以使用<algorithm>
标头中的std::copy()
函数,这是在C ++中复制任何类型容器的首选方法。 用vector
:
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);
再次使用数组,如果你想要更多的C风格,你可以使用来自<cstdlib>
的C标准库函数memcpy()
:
memcpy(valuesToGrab, updatingValues, 361 * sizeof(int));
// Destination; Source; Number of bytes.
使用memcpy()
(和它的堂兄, memmove()
),你必须小心你要复制的元素的大小; 如果你说361
而不是361 * sizeof(int)
,你将复制361个字节,而不是361个int
'的字节值 - 一个很大的区别。
首先,我不认为这会做你想要的,因为valuesToGrab = updatingValues;
将覆盖你的valuesToGrab
外循环的每个循环。
假设您确实想要这样做,并且您不想更改为向量:
std::copy(updatingValues, updatingValues+361, valuesToGrab);
会做的。 您可以像任何std :: algorithm中的std :: container一样处理普通数组,指针计为随机访问迭代器。
重新考虑你的设计,你不应该“修剪”,你可能不需要复制。
请记住,数组是用C和C ++中的指针实现的。
特别是堆栈上的数组可以显示为指向内存中具有您为数组请求的容量的常量位置的指针。 这个内存在堆栈中。 当您尝试valuesToGrab = updatingValues
,你可以认为这是试图的地址复制updatingValues
到变量valuesToGrab
。 这不是尝试深层复制,您似乎正在尝试。 但是, valuesToGrab
指向内存中的常量位置,无法更新。 该标准稍微具体一点,并明确禁止数组的分配,这就是为什么你得到你所看到的特定错误。
您将需要使用循环或类似std::copy
或C的memcpy
来将值从一个数组复制到另一个数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.