C++ 使用指针调整类数组的大小
我的程序中有这段代码。我想创建一个类Ship的数组,它的大小在满的时候会改变。之前我初始化了SIZE=3,Ship*list=newship[SIZE]。当我运行它时,它在我的数组已满后停止工作,这应该执行以下代码:C++ 使用指针调整类数组的大小,c++,C++,我的程序中有这段代码。我想创建一个类Ship的数组,它的大小在满的时候会改变。之前我初始化了SIZE=3,Ship*list=newship[SIZE]。当我运行它时,它在我的数组已满后停止工作,这应该执行以下代码: if(count >= SIZE) { Ship *hold = new Ship[SIZE]; // to hold for a while value of list for(int q = 0; q < SIZE; q++)
if(count >= SIZE)
{
Ship *hold = new Ship[SIZE]; // to hold for a while value of list
for(int q = 0; q < SIZE; q++)
hold[q] = list[q];
delete[] list; //delete list
Ship *list = new Ship[SIZE + 10]; //set a new size for list
for(int q = 0; q < SIZE - 10; q++)
list[q] = hold[q];
delete[] hold;
}
不需要等待,只需将旧列表复制到新列表即可
但是代码中真正的错误是,你没有改变大小,但你认为它已经改变了
Ship *list = new Ship[SIZE + 10]; //set a new size for list
for(int q = 0; q < SIZE - 10; q++)
应该是
Ship *list = new Ship[SIZE + 10]; //set a new size for list
for(int q = 0; q < SIZE; q++)
...
SIZE += 10;
list = new Ship[SIZE + 10]; //set a new size for list
这是您的代码,所有错误都已修复
if(count >= SIZE)
{
Ship *new_list = new Ship[SIZE + 10]; //make the new list
for(int q = 0; q < SIZE; q++) //copy from the old list
new_list[q] = list[q];
delete[] list; //delete the old list
list = new_list; //use the new list
SIZE += 10; //set a new size for list
}
向量稍微容易一点,实际上要容易得多。你应该使用它。使用指针时要小心
// -------------- include directives:
#include <iostream>
#include <string>
// -------------- your class definitions
class ship {
public:
ship() {}
virtual ~ship() {}
};
// ------- declaration state
#define ADDITIONAL_RESERVED_SLOTS (10)
unsigned int arraySize{3};
unsigned int shipCount{}; // initial with zero count
ship* shipArray{nullptr}; // its very important to fill pointers with null for initialialization
void addNewShip(const ship& new_ship); // function prototype
使用如下所示的定义通用函数将新装运添加到阵列中:
void addNewShip(const ship& new_ship) {
if(++shipCount > arraySize) {
arraySize += ADDITIONAL_RESERVED_SLOTS;
// allocate new array
ship* pNewShipArray = new ship[arraySize];
// zero allocated memory for pervent from mistakes
std::memset(pNewShipArray, 0, (sizeof(ship) * arraySize));
// copy previuse data in to the new array
std::memcpy(pNewShipArray, shipArray, (sizeof(ship) * (shipCount - 1)));
// delete previuse array
delete[] shipArray;
// now you can use shipArray
shipArray = pNewShipArray;
}
// add new ship in to your array
shipArray[shipCount - 1] = new_ship; // be carefull!! if you have pointers in your ship class you most handle them too.
}
你有没有考虑过?向量是一种方法,但是如果你-删除[]列表;将其复制到新阵列后,只需要一个复制循环。例:船*舱=新船[尺寸];forint q=0;首先读取一个名为list的东西,然后创建一个新的list变量,该变量不是您从中读取的列表,并填充它。新列表从范围中退出,这意味着您将永远无法再使用它。std::vector-将解决所有这些问题。你为什么不用它?std::vector的全部目的是使数组具有可调整大小的功能。它起作用了,但只是想知道,list=new\u list意味着list将与new\u list共享相同的内存位置,对吗?这就是我以后不需要删除新列表的原因吗?正是这个原因。因为list和new_list在list=new_list之后指向同一内存,如果删除了new_list,则将无法再使用list。回答问题时,请尝试在代码之前添加一些解释。
// -------------- include directives:
#include <iostream>
#include <string>
// -------------- your class definitions
class ship {
public:
ship() {}
virtual ~ship() {}
};
// ------- declaration state
#define ADDITIONAL_RESERVED_SLOTS (10)
unsigned int arraySize{3};
unsigned int shipCount{}; // initial with zero count
ship* shipArray{nullptr}; // its very important to fill pointers with null for initialialization
void addNewShip(const ship& new_ship); // function prototype
// ------- allocate array dynamicly with 3 slot
ship* shipArray = new ship[arraySize];
// zero allocated memory for pervent from mistakes
std::memset(shipArray, 0, (sizeof(ship) * arraySize));
void addNewShip(const ship& new_ship) {
if(++shipCount > arraySize) {
arraySize += ADDITIONAL_RESERVED_SLOTS;
// allocate new array
ship* pNewShipArray = new ship[arraySize];
// zero allocated memory for pervent from mistakes
std::memset(pNewShipArray, 0, (sizeof(ship) * arraySize));
// copy previuse data in to the new array
std::memcpy(pNewShipArray, shipArray, (sizeof(ship) * (shipCount - 1)));
// delete previuse array
delete[] shipArray;
// now you can use shipArray
shipArray = pNewShipArray;
}
// add new ship in to your array
shipArray[shipCount - 1] = new_ship; // be carefull!! if you have pointers in your ship class you most handle them too.
}