简体   繁体   English

如何在指针数组中插入对象

[英]How to insert an object in the array of pointers

I have an array of pointers: 我有一个指针数组:

Hotel *hotels[size];
  for (int i = 0; i < size; ++i)
    hotels[i] = new Hotel();

And I want to insert an object in this array after some object with name I know: 我想在这个数组中插入一个对象,该对象的名称如下:

cin >> tmp_name;
for (int i = 0; i < size; i++) {
  if (hotels[i]->get_name() == tmp_name) {
    hotels[size] = new Hotel();
    size += 1;
    Hotel *tmp_hotel;
    tmp_hotel = hotels[i+1];
    hotels[i+1]->fillHotel();
    for (i = i + 2; i < size; i++) {
      hotels[i] = tmp_hotel;
      tmp_hotel = hotels[i+1];
    }
    break;
  }
}

What I do wrong? 我做错了什么?

UPD: My solution: UPD:我的解决方案:

cin >> tmp_name;
for (int i = 0, j = 0; i < size; i++, j++) {
    new_hotels[j] = hotels[i];
    if (hotels[i]->get_name() == tmp_name) {
        new_hotels[j+1]->fillHotel();
        ++j;
        system("clear");
    }
}

hotels[size] = new Hotel();
++size;
for (int i = 0; i < size; i++) {
    hotels[i] = new_hotels[i];
}

I can see different errors in your code. 我可以在您的代码中看到不同的错误。


For example: 例如:

Hotel *hotels[size];

size should be a constant expression and something let me think this is not the case. size应该是一个常量表达式 ,让我认为事实并非如此。 VLA are not part of the C++ standard. VLA不是C ++标准的一部分。 In short you cannot allocate dynamic memory on the stack. 简而言之,您无法在堆栈上分配动态内存。 The proper initialization should be: 正确的初始化应为:

Hotel* hotels = new Hotel*[size];

The line in the loop: 循环中的行:

hotels[size] = new Hotel();

you're actually accessing out of bounds of your array: size index is some memory is not included in your array and this will produce an undefined behaviour . 您实际上正在访问数组的边界: size索引是数组中未包含一些内存,这将产生未定义的行为


Another strange line is the following: 另一个奇怪的行是:

size += 1;

Despite the fact that confirms size is not a constant, you cannot increase your size of vector simply changing that variable. 尽管确认size不是常数的事实,但仅更改该变量就无法增加向量的大小。 You're actually just changing a variable size , but the allocated memory for your array will be the same. 实际上,您实际上只是在更改变量size ,但是为数组分配的内存将相同。


How resolve? 怎么解决?

In order in increase (or change) the size of an array, the solution is almost always to create a new array, copy the old one. 为了增加(或更改)数组的大小,解决方案几乎总是创建一个数组,然后复制旧数组。 In your case that solution is pretty reasonable because you should copy just pointers and not entire objects. 在您的情况下,该解决方案非常合理,因为您应该只复制指针而不是整个对象。

There are a lots of question on SO where this topic is, for example here . 这里有很多问题上那么,这个话题,例如这里

Despite of that, I strongly suggest you to use the most practical alternative, that is to use a real C++ code . 尽管如此,我强烈建议您使用最实用的替代方法,即使用真正的C ++代码

The most efficient class is std::vector which is a C++ way to handle dynamic array. 最有效的类是std::vector ,它是处理动态数组的C ++方法。

Finally, you should also consider the std::unique_ptr<T> class to handle dynamic memory and pointers. 最后,您还应该考虑使用std::unique_ptr<T>类来处理动态内存和指针。

The final solution will be a class: 最终的解决方案将是一个类:

std::vector<std::unique_ptr<Hotel>> hotels;

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

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