简体   繁体   English

使用指向结构C ++的指针创建动态分配的数组

[英]Create Dynamically Allocated Array with Pointers to Structs C++

So I currently have a simple struct (linkedlist) that I will be using in a HashMap: 因此,我目前有一个将在HashMap中使用的简单结构(链表):

struct Node {
    std::string key, value;
    Node* head;
}

I'm currently trying to dynamically allocate an array with pointers to each struct. 我目前正在尝试动态分配带有指向每个结构的指针的数组。 This is what I have right now ... 这就是我现在所拥有的...

Node* nodes = new Node[100]

I understand this allocates an array of 100 nodes into memory (which I will have to delete later on); 我知道这会在内存中分配一个由100个节点组成的数组(稍后将需要删除); however, upon iteration to try to transverse these nodes (which I an implementing as a linked list)... 但是,在迭代时尝试横穿这些节点(我将其实现为链接列表)...

for (int x = 0; x < 100; x++) {
    Node current = nodes[x]; // Problem is I wanted an array to node pointers. This is not a pointer.
    while (current != nullptr) { // this isn't even legal since current is not a pointer.
        // DO STUFF HERE
        current = current.next; // This is not a pointer access to a method. I'm looking to access next with current->next;
    }
}

Hopefully I was clear enough. 希望我足够清楚。 Can someone how to allocate a dynamic array of pointers to structs? 有人可以分配动态的结构指针数组吗? So far I'm able to dynamically allocate an array of structs, just not an array of pointers to structs. 到目前为止,我已经能够动态分配结构数组,而不是结构指针数组。

There are two approaches. 有两种方法。 Either you allocate an array of structures and introduce one more pointer that will point to the element in the array that will play the role of the head. 要么分配一个结构数组,然后再引入一个指针,该指针将指向将充当头部角色的数组中的元素。

For example 例如

Node *head = nodes;

(in this case head points to nodes[0]) (在这种情况下,头部指向节点[0])

After the list will not be needed you have to delete it using operator 在不需要列表之后,您必须使用运算符将​​其删除

delete [] nodes;

Or you can indeed to allocate an array of pointers to the structure like this 或者您确实可以分配一个指向这样的结构的指针数组

Node **nodes = new Node *[100];

But in this case each element of the array in turn should be a pointer to a dynamically allocated object; 但是在这种情况下,数组的每个元素又应该是指向动态分配对象的指针;

And to delete the list you at first have to delete each object pointed to by elements of the array for example in a loop 要删除列表,您首先必须删除数组元素所指向的每个对象,例如在循环中

for ( int i = 0; i < 100; i++ ) delete nodes[i];

and then to delete the array itself 然后删除数组本身

delete [] nodes;

It is a good idea to initialize each element of the array with zeroes when the array is allocated for example 例如,分配数组时,最好用零初始化数组的每个元素

Node **nodes = new Node *[100]();

I suggested you this structure: 我建议您使用以下结构:

class myList {

struct Node {
string value;
Node* next;
}

/*Public methods .. Add/Set/Get/Next/isEmpty.. etc ... */
Node* head, *tail;
};

in main: myList* lis = new myList[number]; 在主要: myList* lis = new myList[number]; then you have number of lists! 那么您就有列表数量了! and do all work in class by method's and operators, like if you want the next node just call lis[0].getNext(); 并按方法和运算符在类中完成所有工作,例如,如果您要下一个节点,只需调用lis[0].getNext(); if you want to skip current node do lis[0].Next(); 如果要跳过当前节点,请执行lis[0].Next(); ... etc .. ...等..

this how to work, what you try to do is looks like C program! 这种工作方式,您尝试做的就像是C程序!

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

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