1

这样的东西有什么用和解释?:

int capacity;
int** number;
this->number = new int*[this->capacity];

我正在为考试而学习,在考试中,他们提出了使用指针到指针对象并从中创建动态数组的要求。有两个班级;钱包和钱包管理员。在解决方案中,他们在 WalletKeeper 的头文件中执行了此操作:

private:
    Wallet** wallets;
    int capacity;
    int size;
    /*other stuff below this*/

在构造函数中:

WalletKeeper::WalletKeeper(int capacity)
{
    this->capacity = capacity;
    this->size = 0;
    this->wallets = new Wallet*[this->capacity];
    this->initiate();
}

我了解这样的基本动态数组:

Wallet * wallets = new Wallet[capacity];

这意味着您要创建一个指针,该指针指向创建此钱包数组的内存位置,因此您可以更改这些内存插槽的内容。但是你为什么要创建一个指向指针数组的指针呢?什么用途?

钱包没有自己的数组,否则我会理解它,因为我读了这个:初始化指向多维数组的动态指针的正确方法?

教授们正在休假,直到进一步的麻烦。

4

3 回答 3

3

指针数组有很多用途。

  1. 重新排序。假设您要对数组中的对象重新排序。处理指针比移动整个对象要快得多。
  2. 动态分配。您可以单独删除或分配每个对象。
  3. 重新分配和性能。假设您想增加数组的大小。重新分配实际对象可能会导致不同类型的问题(无效)。然而,重新分配指针数组或多或少没有麻烦,而且速度也快得多。
于 2016-08-16T15:01:23.800 回答
1

The basic idea is that it allows you to create an "array of arrays". It has a narrow advantage over a matrix in that it allows you to have differently sized sub-arrays, but a disadvantage in that the memory of all objects is no longer contiguous across the entire array.

Wallet ** w_ptr_ptr = new Wallet*[capacity];
for(int i = 0; i < capacity; i++) {
    w_ptr_ptr[i] = new Wallet[i+1];
}

for(int i = 0; i < capacity; i++) {
    for(int j = 0; j < i+1; j++) {
        w_ptr_ptr[i][j] = Wallet(/*...*/);
    }
}

Note that in that code, w_ptr_ptr[0] has a differently sized array than w_ptr_ptr[1].

As I alluded to in my comment though, your professor shouldn't be teaching like this. Because this code requires manual memory cleanup, and doesn't have any capacity to do automatic bounds checking, the code you should be using is:

std::vector<std::vector<Wallet>> wallets;

for(int i = 0; i < capacity; i++) {
    wallets.emplace_back(i+1); //Will automatically create a i+1-sized array.
}

for(int i = 0; i < wallets.size(); i++) { //Note I'm able to query the size here!
    for(int j = 0; j < wallets[i].size(); j++) { //Again I can query the size!
        wallets[i][j] = Wallet(/*...*/);
    }
}
于 2016-08-16T15:05:48.430 回答
0

指向指针对象的指针通常用于矩阵实现。正如您在问题中所建议的那样,确实像指向对象的指针实现了动态数组:

Wallet* wallets = new Wallet[capacity];

使 wallets 将数组的第一个位置指向 num-capacityWallet对象。

[Wallet-obj] [Wallet-obj] [Wallet-obj] ... [Wallet-obj]
     0           1             2           capacity - 1

指针的指针,如:

Wallet** wallets = new Wallet*[capacity];

创建一个钱包指针数组:

[Wallet-pointer] [Wallet-pointer] [Wallet-pointer] ... [Wallet-pointer]
        0              1                 2                capacity-1

向量中的每个指针都应该指向一个动态数组。

我将尝试“绘制”一个表示:

 [0][wallet-pointer]  ----> [0][wallet obj] [1][wallet obj] ... [capacity-1][wallet obj]
 [1][wallet-pointer]  ----> [0][wallet obj] [1][wallet obj] ... [capacity-1][wallet obj]
 ...
 [capacity-1][wallet-pointer]  ----> [0][wallet obj] [1][wallet obj] ... [capacity-1][wallet obj]

所以为了访问一个对象,你应该使用类似的东西:

wallets[0][2];

这意味着访问指针数组中的第一个指针,使用对象的第一个“行”访问该行的第三个对象。

所以你可以想象,你有一个矩阵,因为你有一个容量为 n 的动态数组,比如容量为 n 的行。

笔记

当您实例化一个指针数组时,您必须初始化它们中的每一个。这是一个完整的代码:

Wallet** wallets = new Wallet*[capacity];

// Now for each pointer you have to allocate a dynamic array of n-elements.
for (size_t i = 0; i < capacity; ++i) {
  wallets[i] = new Wallet[capacity];
}

释放阶段也是如此:

// First of all, deallocate each object in each dynamic array:
for (size_t i = 0; i < capacity; ++i) {
  delete[] wallets[i];  // wallets[i] is a dynamic array to deallocate
}

// Finally deallocate the dynamic array of poiter
delete[] wallets;
于 2016-08-16T15:08:53.587 回答