如何指向一个动态数组的东西里的数组? [英] How to point an array inside a dynamic array to something?

查看:119
本文介绍了如何指向一个动态数组的东西里的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构是这样的:

struct Key_Node
{
    int key;
    struct Package_Node *next_package;
};

我将创建一个动态数组结构Key_Node arrayMain [X] ,其中 X的值将由用户输入,并根据它,我将创建动态数组。

I will create a dynamic array "struct Key_Node arrayMain[X]" where the value of X will be entered by the user and depending on it I will create the dynamic array.

由于我不知道数组的大小,显然我不能指向动态数组的东西的每个指针。因此,我将不得不在这里做什么?

Since I don't know the size of the array, obviously I can not point each pointer of the dynamic array to something. So what will I have to do here?

我有另一个结构看起来像这样。

I have another struct that looks like this.

struct Package_Node
{
    int bar_code;
    float package_weight;
    struct Package_Node *next_packaged;
};

Key_Node mainArray [dynamicvalue]

package_node totalPackages [dynamicvalue]

都是为了一个动态数组和链表。我将创建随机包并将使用哈希表的方法对其进行排序。如果我的 X 3,我的随机酒吧code是10,我会做 10%3 这结果1,所以随机 package_node 将被添加到 mainArray [1] 的名单将增长这样。

are a dynamic array and linked list in order. I will be creating random packages and will sort them using hashtable methods. If my X is 3 and my random barcode is 10, I will be doing 10 % 3 which results 1, so the random package_node will be added to mainArray[1] and the list will grow like that.

推荐答案

看来你需要一点点帮助,让你开始。看起来你被要求实施哈希表。在 C 使用的malloc 来分配动态存储在 C ++ 使用,看到的在什么情况下,我使用malloc VS新?

It seems you need a little help to get you started. Looks like you are being asked to implement a hash table. In C you use malloc to allocate dynamic storage, in C++ you use new, see In what cases do I use malloc vs new?

这应该让你开始,希望它帮助。

This should get you started, hope it helps.

#include <iostream>
struct Key_Node
{
    int key;
    struct Package_Node *next_package;
};

struct Package_Node
{
    int bar_code;
    float package_weight;
    struct Package_Node *next_packaged;
};

int main() {
    size_t X;
    std::cout << "Enter X: ";
    std::cin >> X;
    Key_Node *mainArray = new Key_Node[X];

    for(size_t i=0; i<X; ++i) {
        mainArray[i].next_package = NULL;
    }

    //-- Rest of your program goes here

    delete [] mainArray;

    return 0;
}

这篇关于如何指向一个动态数组的东西里的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆