简体   繁体   English

处理结构列表向量

[英]Dealing with vector of list of struct

I read text file as: 我将文本文件读取为:

std::vector< std::list< struct> > std :: vector <std :: list <结构>

My data in the form: 我的数据形式为:

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

Where each line consist of some integer numbers and each integer number has 2 real numbers, 每行包含一些整数,每个整数具有2个实数,

for example: in the first line, integer number 1 has to real number 0.933 , and 0.9 例如:在第一行中,整数1必须为实数0.9330.9

This the code for scanning data: 此代码用于扫描数据:

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);
    }

}

Now I successfully stored the data in the text file, in the map data which is vector of list of strcut 现在,我成功地将数据存储在文本文件中,在地图data ,该data是strcut列表的向量

BUT I didn't succeed in dealing with vector of list of strcut (data) to do the following: 但是我没有成功处理strcut列表向量(数据)来执行以下操作:

1- create map namely FFISupp such that: 1-创建地图即 FFISupp ,以便:

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

For example: 例如:

since the integer number 1 presents in the data sets in three positions, the total probability for integer number 1 =0.933 + 0.906 + 0.933 = 2.772 由于整数1在数据集中出现在三个位置,因此整数1的总概率= 0.933 + 0.906 + 0.933 = 2.772

==> The result of FFISupp ==> FFISupp的结果

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

FFISupp (6,1.659)

2- create map namely AssociatedItem such that: 2-创建地图,即 AssociatedItem 这样:

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

associated items means, for example, the integer number 1 presents in the dataset with other integer number like (2,3,4,5,6) 关联项意味着,例如,数据集中存在的整数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- delete all item that has the result of sum of its probabilities < threshold from FFISupp and update both FFISupp and AssociatedItem 3-从 FFISupp 删除所有结果的总和<阈值的 FFISupp 并更新 FFISupp AssociatedItem

for example , if two items 3 , and 6 have total probabilities < threshold, then, I will update FFISupp 例如 ,如果两项36总概率小于阈值,那么我将更新FFISupp

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

also update AssociatedItem 同时更新AssociatedItem

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

This my try: 这是我的尝试:

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;
    }
}

as i only understand what you meant in stage #1 - i'll help only it. 因为我只了解您在第1阶段中的意思-我只会提供帮助。

you code - as shown below should iterate over all the data vector elements - therefore the stop condition should be simply data.size() . 您的代码-如下所示,应该遍历所有data向量元素-因此,停止条件应该只是data.size()

Stating data.size() - 1 reminds me of C array... well. 声明data.size() - 1我想起C数组...好吧。 an std::vector is not an array, and by iterating until data.size() - 1 you lose the last item. 一个std::vector不是数组,并且通过迭代直到data.size() - 1您丢失了最后一项。

I don't understand what stage#2 and stage#3 goal are. 我不明白第二阶段和第三阶段的目标是什么。

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

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