如何正确使用动态数组?

问题描述 投票:1回答:2

因此,我正在尝试为程序使用动态数组,但我不知道我是否做对了。我应该使用const int还是仅使用int?

int size = 1;
    int *num1 = new int [size];
    int *num2 = new int [size]; 

const int size = 1;
        int *num1 = new int [size];
        int *num2 = new int [size]; 
c++ dynamic-arrays
2个回答
6
投票

如果大小是要从中创建“数组”的编译时常量,则建议使用std::array,如

std::array

如果在编译时未知大小,则使用std::array<int, 1> num1; ,如

std::vector

如果您真的have使用显式动态分配,则选择std::vector,例如std::vector<int> num1(size); ,例如

smart pointers

[如果变量std::unique_ptr应该是std::unique_ptrauto num1 = std::make_unique<int[]>(size); ,完全没有限定,或者如果应该在分配时直接使用文字值,那么它实际上取决于用例,值的可用性,和个人喜好。

另外,对于size,您应该使用size类型而不是const。大多数程序员会立即理解,当您使用constexpr时,该变量用于存储大小。否则,如果使用size_t,则可以是任意整数值。此外,intunsigned类型,因此它不允许负数,这是造成问题的一种原因。


1
投票

我已经看到您现在对此提出一些问题,所以我想向您展示必须调整动态数组的大小与使用size_t的区别,后者将您想要的所有功能打包在一个动态-大小的连续内存块。

以下代码是如何增加动态数组以容纳用户输入的方法。我们不知道用户要输入数字的时间,因此每次输入新数字时,我们都必须保持调整大小。

int

我们可以使用size_t执行相同的操作:

std::vector

[请注意,在int number = 0; std::size_t array_size = 0; // we need to track the size of the thing int *array = nullptr; // nothing in here yet std::cout << "Enter a number, non-number to exit: "; while (std::cin >> number) { // we need to request more memory ++array_size; int *new_array = new int[array_size]; // we have to copy the old array to the new array // fun note: as pointed out in the comments below, using memcpy on // either src or dest == nullptr is undefined behavior. Just goes to // show how hard it is to get something like this correct. // Don't do this when we have perfectly good STL containers! std::memcpy(new_array, array, (array_size - 1) * sizeof(int)); // delete the old array, if it exists (we can safely call delete on a nullptr) delete[] array; // assign the new block of memory to array array = new_array; // add the retrieved element to array array[array_size - 1] = number; std::cout << "Enter a number, non-number to exit: "; } std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // output the array for (std::size_t i = 0; i < array_size; i++) { std::cout << array[i] << "\n"; } // all done, delete the memory that was allocated delete[] array; array = nullptr; // not strictly required, but can prevent us from accidentally deleting the same block of memory twice, which would be bad 示例中,我将输出std::vector的内容两次,只是为了表明我们有一个选项可以遍历int number; std::vector<int> vec; // this is a vector that holds ints, it tracks its own size and memmory std::cout << "Enter a number, non-number to exit: "; while (std::cin >> number) { vec.push_back(number); // all done std::cout << "Enter a number, non-number to exit: "; } std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); for (std::size_t i = 0; i < vec.size(); i++) { std::cout << vec[i] << "\n"; } // or we can used range-based for loops, which are awesome for (auto& v : vec) { std::cout << v << "\n"; } 不可用的向量。我们不需要跟踪内存。我们不需要std::vectorstd::vector。当当前程序作用域退出时(例如,如果这是一个函数),将调用int *的析构函数,并为我们清除内存。

使用矢量!!

© www.soinside.com 2019 - 2024. All rights reserved.