简体   繁体   English

复制阵列然后删除原始

[英]Copy array then delete original

I have an array of a structure (with the parameters of name and number), and the initial array takes in elements from a document that I've made. 我有一个结构数组(带有名称和数字的参数),并且初始数组接收了我制作的文档中的元素。 The initial list size starts at 1000. When the list fills up, I call another method that I'm struggling with. 初始列表的大小从1000开始。列表填满后,我调用了另一个正在苦苦挣扎的方法。 I would like for it to copy the data into a new array that doubled the size, and then delete the old array. 我想将其复制到一个两倍大小的新数组中,然后删除旧数组。

If I name it: array1 and array2, I have my program use array1 throughout. 如果我将其命名为:array1和array2,则我的程序始终使用array1。 I need help with the pointers that would get array2 to work as array1. 我需要有关将array2用作array1的指针的帮助。

Is there a way to copy the array to a temp array of the same or new size, and then remake the initial array reassigning back to that? 有没有一种方法可以将数组复制到相同或新大小的临时数组,然后重新制作初始数组并重新分配给该临时数组? For this exercise, I can't use vectors. 对于本练习,我不能使用向量。 While I know how to use them, and that they solve this issue while being better, I'm trying to do it with only arrays. 虽然我知道如何使用它们,并且它们可以在改善性能的同时解决此问题,但我尝试仅使用数组来实现。

using namespace std;

struct Information {
  char functionality;
  int SSN;
  string name;
};

int numPeople = 1000;

//Gets called if the initial array (whatever size) is filled
void doubleArray(Information *array){
  numPeople = numPeople * 2;
  //Will now be the doubled array size
  Information temp[numPeople]
  for(int i = 0; i < numArray; i++){
    temp[i].SSN = array[i].SSN;
    temp[i].name = array[i].name;
  }
  //Normally makes it crash
  delete[] array;
}

edit: This is what I currently have 编辑:这是我目前拥有的

void doubleArray(Information *person){
  numPeople = numPeople * 2;
  Information* temp = new Information[numPeople];
  memcpy(temp, person, numPeople);
  delete[] person;
  person = temp;
}

It gets to numPeople = 1000 (the initial list size) but then crashes shortly after. 它达到numPeople = 1000(初始列表大小),但不久后崩溃。 Is the doubling array correct? 加倍数组正确吗?

Arrays are fixed size. 数组是固定大小的。 You cannot change the capacity of the original array. 您无法更改原始阵列的容量。

{ Use std::vector } { 使用std::vector }

You can have a pointer to an array. 您可以有一个指向数组的指针。 And use the same pointer. 并使用相同的指针。 When the array is full, you can allocate another array, copy old array items to new array, delete the old array and assign your array pointer to the new array. 当阵列已满时,您可以分配另一个阵列,将旧的阵列项目复制到新的阵列,删除旧的阵列,并将阵列指针分配给新的阵列。

{Did I mention std::vector ?} {我没有提到std::vector吗?}

By the way, there is a data structure that performs resizing as necessary. 顺便说一句,有一个数据结构可以根据需要执行大小调整。 If I recall correctly, it is std::vector . 如果我没记错的话,它是std::vector Try it out. 试试看。 :-) :-)

Assuming you are using std::array (which you should be), then copying the array is very easy. 假设您正在使用std::array (应该是),那么复制数组非常简单。

std::array<myStruct, 1000> array1{};
std::array<myStruct, 2000> array2{};
// codes...
std::copy(array1.begin(), array1.end(), array2.begin())

However, this is a specific scenario in which you only use these two arrays. 但是,这是仅使用这两个数组的特定方案。 It will not dynamically double the size of the array as you simply cannot do this dynamically with stack-based arrays, just like c arrays[]. 它不会动态地使数组的大小增加一倍,因为您根本无法像基于c数组[]那样使用基于堆栈的数组来动态地做这件事。

What you can, and should, be using is std::vector<myStruct> . 您可以并且应该使用的是std::vector<myStruct> This will dynamically grow as you need it. 这将根据需要动态增长。 Until you provide us with code and a more specific issue, this is the best advice that I can offer with the information provided. 在您为我们提供代码和更具体的问题之前,这是我可以根据所提供的信息提供的最佳建议。

If you aren't allowed to use std::vector , as one of your comments stated, then you'll want to look at dynamic allocation. 如果您不允许使用std::vector ,如您的评论之一所述,那么您将要看一下动态分配。

size_t sz = [whatever];

// Dynamically allocate an array of size sz.
T* T_array = new T[sz];

// Do whatever...

delete[] T_array; // new[] needs to be paired with delete[].
T_array = nullptr; // Not strictly necessary, but a good idea if you have more code after.

As the size doesn't need to be constant for a dynamic array, this will allow you to allocate memory as necessary. 由于动态数组的大小不需要恒定,因此您可以根据需要分配内存。 You can then use std::copy() to copy data from one array to the other, as Goodies mentioned . 然后,您可以使用std::copy()将数据从一个数组复制到另一个数组,如Goodies所述

[For more information on dynamic allocation, see here .] [有关动态分配的更多信息,请参见此处

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

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