更新:在底部回答。
嗨,大家好,
如何初始化“指向结构的指针数组”?问题是,数组是成员变量,传递给构造函数中数组声明的大小是变量实体。
typedef struct Node {
string key;
int value;
struct Node * left;
struct Node * right;
}doubly;
class myHashStrKey{
private:
size_t hashsize;
doubly * table[];
public:
myHashStrKey(){
hashsize = ((size_t)-1);
doubly * table[hashsize];
memset(table,NULL,hashsize);// This is giving segmentation fault
}
};
//Called constructor; myHashStrKey sss = myHashStrKey();
在这里,我希望表是指向双节点的指针数组,并且我希望将所有指针初始化为 NULL 。这段代码有什么问题吗?还有什么其他更好的方法来执行上述操作?提前致谢。
更新 :
经过讨论,考虑到尺寸很大,我修改了代码。但是如何用一定数量的 NULL 值填充向量表?我尝试了下面的代码,但它不起作用。
<pre><code>
for(int i =0;i < hashsize;i++){
table.push_back((doubly *)NULL);
}
table.insert(table.begin(),hashsize,NULL);
//Both give invalid static_cast from type `int' to type `doubly*'
</code></pre>
答案更新:
myHashStrKey::table(myHashStrKey::hashsize, static_cast(0));
myHashStrKey::table(myHashStrKey::hashsize);
//Above 2 does not work
for(int i =0;i != myHashStrKey::hashsize;i++){ //lesser than symbol spoils the display
myHashStrKey::table.push_back((doubly *)NULL);
}
//Above works
myHashStrKey::table.insert(myHashStrKey::table.begin(),hashsize,((doubly *)NULL));
//This too works