[英]resize array of pointers c++
假設我有這個
#define T 10
StackOverflow *_stObject[H];
如何將該數組“調整”為20? 我不能使用向量。 我必須將11個位置復制到另一個20個位置的指針數組,但是那樣我將無法在其他函數中使用此對象。
該對象存儲數據,當它裝滿時,我需要找到一種繼續添加數據的方法。
我怎樣才能做到這一點?
好的,這里有更多信息,因為它不起作用。 我制作了方法extendArray(),但是當我使r = temp時出現錯誤
這是一個計算器,Reg類將信息存儲在計算器中進行的操作。 對象“R”專賣店10個人作戰,我要擴大這個數組的指針,如果我做10個多操作。
我收到的錯誤消息是r = temp,它說:'Reg * [20]'到'Reg * [10]'|的分配類型不兼容|
#define T 10
class Calculator
{
public:
Calculator();
Calculator(NumComp&,NumComp&);
~Calculator();
void printVisor();
void setCalculator(const NumComp&,const NumComp&);
void menu();
void help();
void clean();
void run();
void extendArray();
private:
NumComp n1, n2;
Reg *r[T];
int _tMax;
};
void Calculator::extendArray()
{
Reg *temp[T*2];
for(int i = 0; i < 5; i++){
temp[i] = new Reg;
temp[i] = r[i];
delete r[i];
}
r = temp;
_tMax *= 2;
}
您必須創建一個新數組並進行深拷貝。
StackOverflow * array = new StackOverFlow[(H * 2)];
for (int i = 0; i < H; i++)
arrary[i] = _stObject[i];
現在,您有了一個數組,其大小是原始數據的兩倍,並且包含來自原始數據的所有數據。
如果您的作業/作業表明您不能使用任何STL容器,則可以執行以下操作
#define T 10
class NumComp {
// ---
};
class Reg {
// ---
};
class Calculator
{
public:
Calculator();
Calculator(NumComp&,NumComp&);
~Calculator();
void printVisor();
void setCalculator(const NumComp&,const NumComp&);
void menu();
void help();
void clean();
void run();
void extendArray();
private:
NumComp n1, n2;
Reg** r;
int _tMax;
void createArray();
void cleanupArray();
};
Calculator::Calculator() {
createArray();
}
Calculator::Calculator(NumComp&,NumComp&) {
createArray();
}
Calculator::~Calculator() {
cleanupArray();
}
void Calculator::createArray() {
// create a pointer array
r = new Reg*[T];
// initialize to nulls
for (int i = 0; i < T; i++) {
r[i] = 0;
}
_tMax = T;
}
void Calculator::cleanupArray() {
// make sure the contents of r[] are deleted by whoever allocates them
delete [] r;
r = 0;
}
void Calculator::extendArray()
{
Reg** temp = new Reg*[_tMax*2];
// copy contents of r to temp
for(int i = 0; i < _tMax; i++) {
temp[i] = r[i];
}
// initialize rest of temp
for (int i = _tMax - 1; i < _tMax*2; i++) {
temp[i] = 0;
}
// delete what was originally in r
delete [] r;
r = temp;
_tMax *= 2;
}
擴展數組的唯一方法是將其內容復制到更大的數組中。 您不能簡單地將一個較大的數組分配給一個較小的數組(這就是為什么incompatible types in assignment of 'Reg* [20]' to 'Reg* [10]'
得到錯誤incompatible types in assignment of 'Reg* [20]' to 'Reg* [10]'
的原因)。 但是,您可以將任意大小的指向數組的指針分配給另一個(相同類型的)指向數組的指針。 因此,要擴展數組,首先要創建一個較大的數組,將較小數組的內容復制到其中,清理較小的數組,然后將指向較大數組的指針分配給較小的數組。
如果您不能使用std::vector
(這將是最合理的用法),那么您可以編寫具有簡化功能的“矢量”:
class MyVector
{
int size;
int capacity;
StackOverFlow ** data;
public:
MyVector() : size(0), capacity(10), data(new StackOverFlow * [10]) {}
~MyVector() {delete data;}
void duplicateCapacity()
{
capacity *= 2;
StackOverFlow ** tmp = new StackOverFlow * [capacity];
for(int i=0; i<size; i++)
tmp[i] = data[i];
delete data; //<-- here was an error...
data = tmp;
}
void add(StackOverFlow * item)
{
if(size == capacity)
duplicateCapacity();
data[size++] = item;
}
StackOverFlow * get(int index) {return data[index];}
};
有了這個基本功能,您就可以完成工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.