处理结构体列表的向量

Dealing with vector of list of struct

本文关键字:向量 列表 结构体 处理      更新时间:2023-10-16

我读取文本文件为:

std:: vector>

我的数据形式:

1 0.933 0.9   2 0.865 0.6   3 0.919 0.2   4 0.726 0.5 
3 0.854 0.6   5 0.906 0.2   6 0.726 0.5 
1 0.906 0.2   2 0.726 0.5 
1 0.933 0.2   2 0.865 0.5   4 0.919 0.1   5 0.726 0.5   6 0.933 0.9

每行由若干整数组成,每个整数有2个实数,

例如

: 第一行,整数1必须是实数0.9330.9

这是扫描数据的代码:

struct Lines1 {
    int Item;
    float Prob;
    float W;
};
std::istream& operator>>(std::istream &is, Lines1 &d)
{
    return is >> d.Item >> d.Prob>> d.W;
}

float threshold;            
std::map<int, float> FFISupp;
std::map <int, vector <int> > AssociatedItem; 
std::vector<std::list<Lines1>> data;
void ScanData()
{
    ifstream in;
    in.open(dataFile);
    std::string line;
    int i = 0;
    while (std::getline(in, line))
    {
        std::stringstream Sline1(line);
        std::stringstream ss(line);
        std::list<Lines1 > inner;
        Lines1 info;
        while (ss >> info)
        {
          inner.push_back(info);
        }
        data.push_back(inner);
    }
}

现在我成功地将数据存储在文本文件中,在映射data中,这是strcut

列表的向量

BUT我没有成功地处理strcut (data)列表的向量来执行以下操作:

1 -创建地图即 FFISupp 这样:

FFISupp (key = the 6 distinct integer number in the data struct, value = the summation of probabilities for each number)

例如:

由于整数1在数据集中出现在三个位置,所以整数1出现的总概率=0.933 + 0.906 + 0.933 = 2.772

==> FFISupp结果

FFISupp (1, 2.772)
FFISupp (2, 2.456)
.
.
FFISupp (6,1.659)

2 -创建地图即 AssociatedItem 这样:

AssociatedItem (key = 6 distinct integer number, value = the associated items with this number)

关联项表示,例如,整数1在数据集中与其他整数(2,3,4,5,6)一起呈现

    AssociatedItem (1, (2,3,4,5,6)) 
    AssociatedItem (2, (1,3,4,5,6))
    AssociatedItem (3, (1,2,4,5,6))
    AssociatedItem (4, (1,2,3,5,6)) 
    AssociatedItem (5, (1,2,3,4,6)) 
    AssociatedItem (6, (1,2,3,4,5)) 

3-删除所有结果为其概率和的项<阈值from FFISupp并同时更新 FFISupp AssociatedItem

例如,如果36两个项目有总概率<然后,我将更新FFISupp

FFISupp (1, 2.772)
FFISupp (2, 2.456)
FFISupp (4, 1.645)
FFISupp (5,1.632)

也更新AssociatedItem

    AssociatedItem (1, (2,4,5)) 
    AssociatedItem (2, (1,4,5))
    AssociatedItem (4, (1,2,5)) 
    AssociatedItem (5, (1,2,4))  

这是我的尝试:

void Pass()
{
    for (unsigned i = 0; i < data.size() - 1; ++i)
    {
        for (unsigned k = 0; i < data[i].size() - 1; ++k)
        {
            for (unsigned l = k + 1; l < data[i].size(); ++l)
            {
                auto  p1 = make_pair(data[i][k].Item, data[i][k].Prob);
                FFISupp[p1.first] += p1.second;
                AssociatedItem[data[i][k].Item].push_back(data[i][l].Item);
            }
        }
    }

    /*update the FFISupp, and AssociatedItem by  erasing allitems  with  <= Min_Threshold*/
    std::map<int, float> ::iterator current = FFISupp.begin();
    std::map<int, vector <int>> ::iterator current2 = AssociatedItem.begin();
    while (current != FFISupp.end())
    {
        if (current->second <= threshold)
        {
            current = FFISupp.erase(current);
            while (current2 != AssociatedItem.end())
            {
                    current2 = AssociatedItem.erase(current2);
                    ++current2;
            }
        }
    else
    ++current;
    }
}

因为我只明白你在第一阶段的意思-我只会帮助它。

您的代码—如下所示—应该遍历所有data向量元素—因此停止条件应该简单地为data.size()

说明data.size() - 1让我想起C数组…好。std::vector不是数组,迭代到data.size() - 1就会丢失最后一项。

我不明白第二阶段和第三阶段的目标是什么