简体   繁体   English

处理类型为struct的数组

[英]Dealing with array of type struct

I have problem in: Re-Using array of struct 我在以下方面遇到问题:重新使用结构数组

The 1D array (Trans) is global: 一维数组(Trans)是全局的:

struct Transaction
{
    int Item;
    float prob;
    int support;
    Transaction *next;
};

#define ItemNum 1000
Transaction *Trans[ItemNum]; 
Transaction *PairItems[ItemNum][ItemNum];

I initialize Trans as: 我将Trans初始化为:

for (int a = 0; a <= ItemNum - 1; a++)
        Trans[a] = NULL;

Then I fill this array with input from text file. 然后我用来自文本文件的输入填充该数组。 In specific: 具体来说:

i = 0;
    while (!inFile.eof())
    {
        FollowingItem = NULL;
        getline(inFile, line);
        std::stringstream in(line);

        while (in >> a >> b)
        {
                NewItem = new Transaction;
                NewItem->Item= a;
                NewItem->prob = b;
                NewItem->next = NULL;

                if (Trans[i] == NULL)
                    Trans[i] = FollowingItem = NewItem;
                else
                {
                    FollowingItem->next = NewItem;
                    FollowingItem = NewItem;
                }
        }
        i++;
   }  

Then, I print it: 然后,我打印它:

i=0;
while (Trans[i] != NULL)
        {
            while (Trans[i] != NULL)
            {
                cout << Trans[i]->Item << " " << Trans[i]->prob<<" ";
                Trans[i] = Trans[i]->next;
            }
            cout << "\n";
            i++;
        }

Until Now, everything is okay, 到现在为止,一切都还好,

BUT

When I try to use Trans again, I can't, since the array become empty!! 当我再次尝试使用Trans时,由于数组为空,所以我不能!

for example, if I do this code: 例如,如果我执行以下代码:

for (int a = 0; a <= ItemNum - 1; a++)
    for (int b = 0; b <= ItemNum - 1; b++)
    {
        PairItems[a][b] = new Transaction;
        PairItems[a][b]->support = 0;
    }

            int k = 0;
        while (Trans[k] != NULL)
        {
            int l = 0;
            while (Trans[l] != NULL)
            {
                PairItems[k][l]->Item = Trans[k]->Item;
                PairItems[k][l]->prob = Trans[k]->prob;
                PairItems[k][l]->support += 1;
                cout << PairItems[k][l]->Item << " " ;
                Trans[k] = Trans[k]->next;
                l++;
            }
            cout << "\n";
            k++;
        }

the compiler ignores this condition: 编译器将忽略这种情况:

while (Trans[k] != NULL) 而(Trans [k]!= NULL)

because Trans[k]=NULL . 因为Trans [k] = NULL I don't know way! 我不知道!

But when I delete the printing code, Trans[k] != NULL and the compiler enter to the condition and execute the rest!! 但是,当我删除打印代码时, Trans [k]!= NULL ,编译器将输入条件并执行其余部分!

I think the problem associated with the initializing the arrays of structure, but I couldn't find the solution 我认为与初始化结构数组有关的问题,但我找不到解决方案

Please Help 请帮忙

Thanks 谢谢

Your print code modify the array, in particular Trans[i] = Trans[i]->next; 您的打印代码将修改数组,尤其是Trans[i] = Trans[i]->next;

Your print function may be written: 您的打印功能可以写成:

for (int i = 0; Trans[i] != NULL; ++i) {
    for (const Transaction* it = Trans[i]; it != NULL; it = it->next) {
        std::cout << it->Item << " " << it->prob <<" ";
    }
    cout << "\n";
}

BTW, you may use std::vector<std::list<Transaction> > Trans instead of hard-coded array of handwritten list. 顺便说一句,您可以使用std::vector<std::list<Transaction> > Trans代替手写列表的硬编码数组。

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

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