C++ 处理struct类型的数组

C++ 处理struct类型的数组,c++,arrays,struct,C++,Arrays,Struct,我有一个问题:重复使用struct的数组 1D阵列(Trans)是全局的: struct Transaction { int Item; float prob; int support; Transaction *next; }; #define ItemNum 1000 Transaction *Trans[ItemNum]; Transaction *PairItems[ItemNum][ItemNum]; 我将Trans初始化为: for (int a

我有一个问题:重复使用struct的数组

1D阵列(Trans)是全局的:

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

#define ItemNum 1000
Transaction *Trans[ItemNum]; 
Transaction *PairItems[ItemNum][ItemNum];
我将Trans初始化为:

for (int a = 0; a <= ItemNum - 1; a++)
        Trans[a] = NULL;
然后,我打印它:

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++;
        }
i=0;
while(Trans[i]!=NULL)
{
while(Trans[i]!=NULL)
{

cout Item您的打印代码修改数组,特别是
Trans[i]=Trans[i]->next;

您的打印功能可能被写入:

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";
}
for(int i=0;Trans[i]!=NULL;++i){
for(const事务*it=Trans[i];it!=NULL;it=it->next){

std::cout Item您的
i++
在while循环之外!(第二位代码)我指的是文本文件中的当前行/行。我想填充结构Trans的数组,以便:Trans[0]=文件中的项目集(提取并存储文件第一行中的所有元素)作为链表,Trans[1]=文件中的项目集(提取并存储文件第二行中的所有元素)作为链表,依此类推。有些关联:确认那些数组不在
main()
中,尤其是第二个数组,在32位实现中,它的最小值将略小于4MB。如果是
main()的本地数组
这是可行的,您的堆栈比我的堆栈要强大得多。此外,还有比这更简单的方法填充链接表。“直到现在一切正常”否:不要使用
!eof()
检查数据结尾。此外,您在这里使用了很多(原始)指针。谢谢,但我应该使用什么来检查数据结尾(文件)??。我很难选择好的数据结构来存储数据文件,因为文件中的每一行都必须在数组中,所以我使用了链表数组!你有什么建议?非常感谢,我没有注意到我在数组中进行了编写。我不擅长新的编程方式(使用向量和列表)但我很感谢你介绍他们。我相信我必须学习他们,因为我将处理大文件。
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++;
        }
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";
}