简体   繁体   English

我如何在cpp中创建动态数组

[英]how do i create dynamic array in cpp

I have this function: 我有这个功能:

void reverse(int* nums, unsigned int size)

This function is supposed to reverse the values in the array it is getting. 该函数应该反转其获取的数组中的值。

Now for reversing I thought to create another array with the size of the array passed in. Assigning this new one from the end of the original array to the start. 现在,为了进行反转,我想创建一个传入的数组大小的另一个数组。将这个新数组从原始数组的末尾开始分配。

But I am a kind of new in C++, So I don't know how to create dynamic array in the size of the parameter of the function. 但是我是C ++的新手,所以我不知道如何在函数参数的大小上创建dynamic array

It's actually not necessary to allocate a new array here. 实际上,没有必要在此处分配新数组。 See if you can find a way to solve this problem just by rearranging the existing elements in-place. 看看您是否能找到解决此问题的方法,而只是通过重新布置现有元素就可以了。

Given that this seems like it's an exercise with pointers, you can allocate space by using the new[] operator: 假设这似乎是一个使用指针的练习,则可以使用new[]运算符分配空间:

int* auxiliaryArray = new int[size];

You'd then free it by writing 然后您可以通过写来释放它

delete[] auxiliaryArray;

However, this isn't the preferred way of doing this in C++. 但是,这不是在C ++中执行此操作的首选方法。 The better route is to use std::vector , which does all its own memory management. 更好的方法是使用std::vector ,它执行所有自己的内存管理。 That would look like this: 看起来像这样:

std::vector<int> auxSpace(size);

You can then access elements using the square brackets as you could in a real array. 然后,您可以像实际数组一样使用方括号访问元素。 To do this, you'll need to #include <vector> at the top of your program. 为此,您需要在程序顶部#include <vector>

In C++, the recommended way to create an array of variable size would be to use an std::vector 在C ++中,创建可变大小数组的推荐方法是使用std :: vector

#include <vector>
void reverse(int* nums, unsigned int size)
{
    std::vector<int> V(size);
    ...
}

But that approach isn't the best here for performance because it requires additional memory to be allocated of the size of the array, which could be big. 但是这种方法并不是最佳的性能,因为它需要分配额外的内存,大小可能与数组大小有关。 It would be better to start from the outside of the array and swap members one by one that are at mirroring positions (so if the size is 5, swap 0 and 4, then swap 1 and 3 and leave 2 alone). 最好从数组的外部开始,并逐一交换位于镜像位置的成员(因此,如果大小为5,则交换0和4,然后交换1和3并保留2)。 This only requires temporary storage of a single int. 这仅需要单个int的临时存储。

You can do it without the need to create another array: 您可以执行此操作而无需创建另一个数组:

void reverse(int* array, const int size){
    for(int i = 0; i < size / 2; i++){
        int tmp = array[i];
        array[i] = array[size - 1 - i];
        array[size - 1 - i] = tmp;
    }
}


int main(){
    int array[] = {1, 3, 5, 7, 9, 11};
    const int size = sizeof(array) / sizeof(array[0]);
    reverse(array, size);

    for(int i(0); i < size; i++)
        std::cout << array[i] << ", ";

}

As you can see above in the loop you only need to swap the first element (element 0) with the n-1 element and the second one with n-1-1 and son on... 正如您在循环中上面看到的那样,您只需要将第一个元素(元素0)与n-1个元素交换,将第二个元素与n-1-1和son交换。

Remember arrays are indexed from 0 through n-1 . 请记住,数组的索引是从0n-1

If you want to allocate new array which is not practical: 如果要分配新数组不可行:

int* reverse2(int* array, const int size){
    int* tmp = new int[size];

    for(int i(size - 1), j(0); j < size; j++, i--)
        tmp[j] = array[i];

        return tmp;
}


int main(){

    int array[] = {1, 3, 5, 7, 9, 11};

    for(int i(0); i < size; i++)
        std::cout << array[i] << ", ";

    std::cout << std::endl;

    int* newArray = reverse2(array, size);
    for(int i(0) ; i < size; i++)
        std::cout << newArray[i] << ", ";

    std::cout << std::endl;

    delete[] newArray;

    return 0;
}

If you want to use a new array you can, but I think is to kill flies with a cannon. 如果您想使用新的阵列,可以,但是我认为是用大炮杀死苍蝇。

  1. Looks like you are using plain C code and not C++. 看起来您正在使用普通的C代码而不是C ++。 I say that because of the signature of the function. 我之所以这样说是因为功能的签名。 The signature of the function in a common C++ code could be something like this other: 通用C ++代码中的函数签名可能与此类似:

    void reverse(std::vector& items);

  2. You can reverse the current array without a new array, using the current one. 您可以使用当前数组反转当前数组而无需新数组。 You are passing the pointer to the first item of the array, and the content is not constant so that you can modify it. 您将指针传递给数组的第一项,并且内容不是恒定的,因此您可以对其进行修改。 A better signature for the function could be: 该函数的更好签名可能是:

    void reverse(int* const nums, const unsigned int size);

  3. Looks like a pointer problem. 看起来像是指针问题。 Think about the boundaries to iterate the positions of the array. 考虑边界以迭代数组的位置。 Would you need to iterate the whole array? 您是否需要迭代整个数组? Maybe only half array? 也许只有一半数组? ;) ;)

  4. As bonus track, what about to exchange the values without an auxiliar variable? 作为奖励途径,如何在没有辅助变量的情况下交换值? (this is true into this case that we are using the fundamental type int ... remember the binary arithmetic). (在这种情况下,我们使用的是基本类型int ,这是正确的……记住二进制算法)。


array[pos_head] ^= array[pos_tail];
array[pos_tail] ^= array[pos_head];
array[pos_head] ^= array[pos_tail];

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

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