简体   繁体   English

如何将数据从一个阵列复制到另一个没有名称的阵列? (C ++)

[英]How to Copy Data from One Array to Another Without Names? (C++)

I'm working on an assignment right now and have run into a roadblock. 我现在正在做作业,遇到了障碍。 The assignment is an array list in C++ that dynamically expands by a factor of 2 every time it runs out of room to store new elements (initially starts with room for 2 elements). 分配是C ++中的一个数组列表,每次它用完存储新元素时(最初以2个元素的空间开始),它都会动态扩展2倍。 Here is the code I'm working on (some of it is included in a separate .h file provided by the professor, I won't post everything in order to keep this compact). 这是我正在处理的代码(其中一些包含在教授提供的单独的.h文件中,为了保持紧凑性,我不会发布所有内容)。

#include "array_list.h"

//initial size to create storage array
static const unsigned int INIT_SIZE = 2;
//factor to increase storage by when it gets too small
static const unsigned int GROW_FACTOR = 2;
unsigned int    growthTracker = 1;

array_list::array_list()
{
    m_storage = new unsigned int[INIT_SIZE];
    m_capacity = INIT_SIZE;
    m_current = -1;
    m_size = 0;
}

array_list::~array_list()
{
    delete m_storage;
}

void array_list::clear()
{
    delete m_storage;
    m_storage = new unsigned int[INIT_SIZE];
    m_capacity = INIT_SIZE;
    m_current = -1;
    m_size = 0;
}

unsigned int array_list::size() const
{
    return m_size;
}

bool array_list::empty() const
{
    bool A = 0;
    if(m_size == 0)
    {
        A = 1;
    }
    return A;
}

void array_list::insert(const unsigned int val)
{
    m_storage[m_size++] = val;
    m_current = m_size;
}

void array_list::grow_and_copy()
{
    if(m_size == m_capacity)
    {
        new unsigned int[INIT_SIZE * (GROW_FACTOR ^ growthTracker)];
        growthTracker++;
        m_capacity = m_capacity * 2;
    }
    m_storage[m_size++] = val;
}

Now, my problem is trying to figure out how to copy the values of the old, smaller array into the new, larger one. 现在,我的问题是试图弄清楚如何将旧的较小数组的值复制到新的较大数组中。 If I wasn't using dynamic unnamed arrays, this would be very easy to do with a loop, a simple case of "for a certain range, arrayA[i] = arrayB[i]." 如果我不使用动态未命名数组,那么使用循环非常容易,这是一个简单的情况,即“对于一定范围,arrayA [i] = arrayB [i]”。 However, because the arrays are just defined as new unsigned int[], I'm not sure how to go about this. 但是,由于数组只是定义为新的unsigned int [],因此我不确定该如何处理。 There are no names, so I can't figure out how to tell C++ which array to copy into which. 没有名称,所以我不知道如何告诉C ++将哪个数组复制到哪个数组中。 And since the grow_and_copy could be called multiple times, I'm fairly sure I can't give them names, right? 而且由于grow_and_copy可以被多次调用,所以我很确定我不能给他们起名字,对吗? Because then I would end up with multiple arrays with the same name. 因为那样我最终会得到多个具有相同名称的数组。 Can anyone point me in the right direction here? 有人可以在这里指出正确的方向吗? Thanks so much. 非常感谢。

array_list::growList(int increase = GROW_FACTOR)
{
    unsigned int* temp = m_storage;
    m_storage = new unsigned int[m_capacity * increase];
    for (int i = 0; i < m_capacity; i++)
        m_storage[i] = temp[i];
    m_capacity *= increase;
    delete temp;    
}

I don't know if there are other variables you want to change, but this should basically do what you are asking. 我不知道您是否要更改其他变量,但这基本上可以满足您的要求。

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

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