简体   繁体   English

在一个成员变量上合并std :: set

[英]merge std::set on one member variable

My requirement here is to merge the qty of both the sets if the price is same but isImplied bool are different. 我的要求是,如果价格相同但被隐含的布尔值不同,则要合并两个集合的数量。

Current Output: 电流输出:

Price : 100 IsImplied : 0 Qty :10 价格:100现价:0数量:10

Price : 200 IsImplied : 0 Qty : 20 价格:200现价:0数量:20

As the price 100 and 200 were already present in the set the insertion of p3 and p4 is ignored. 由于价格100和200已经存在于集合中,因此将忽略p3和p4的插入。

Desired output: 所需的输出:

Price : 100 IsImplied : 0 Qty :40 (10 + 30) (Qty is merged as P1 and P3 have same price but different isImplied values) 价格:100 IsImplied:0数量:40(10 + 30)(由于P1和P3具有相同的价格,但isImpplied值不同,所以数量合并)

Price : 200 IsImplied : 0 Qty : 60 (20 + 40) (Qty is merged as P2 and P4 have same price but different isImplied values) 价格:200 IsImplied:0数量:60(20 + 40)(数量合并,因为P2和P4具有相同的价格,但isImpplied值不同)

class PriceLevel
{
public:
    int price;
    int qty;
    bool isImplied;

    PriceLevel(int _price, int _qty, bool _isImplied)
    {
        price = _price;
        qty = _qty;
        isImplied = _isImplied;
    }

    friend bool operator<(const PriceLevel &p, const PriceLevel &q);
};

bool operator<(const PriceLevel &p, const PriceLevel &q)
{
    if(p.price < q.price)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    std::set<PriceLevel> s1;

    PriceLevel p1(100,10, false);
    PriceLevel p2(200,20, false);
    PriceLevel p3(100,30, true);
    PriceLevel p4(200,40, true);

    s1.insert(p1);
    s1.insert(p2);
    s1.insert(p3);
    s1.insert(p4);

    set<PriceLevel>::iterator it = s1.begin();

    for(; it != s1.end(); it++)
    {
        cout << "Price: " << it->price << endl;

        cout << "Qty : " << it->qty << endl;

        cout << "IsImplied: " << it->isImplied << endl;

    }
}

If you need to retain the quantity as well, your compare function should use that information. 如果还需要保留数量,则比较功能应使用该信息。 set comparison works on strict weak ordering. 集合比较适用于严格的弱排序。 There are two ways to achieve this. 有两种方法可以实现此目的。 Pick the one that fits your design best. 选择一个最适合您的设计。

1.Instead of keeping a set of PriceLevel itself, keep a map with the key as the Price and value as the quantity. 保持一组PriceLevel本身的1.Instead,保持地图与关键的价格和价值的数量。 Your update function will look something like: 您的更新功能将类似于:

void update(map<int, int> &valueMap, const PriceList &val)
{
    valueMap[val.price] += val.qty;    
}

` 2. Modify the insertion logic in your set. `2.修改集合中的插入逻辑。 Update would look something like: 更新如下所示:

void update(set<PriceList> &valueMap, PriceList val)
{
    auto iter = valueMap.find(val);
    if (iter != valueMap.end())
    {
        val.qty = iter->qty + val.qty;
        valueMap.erase(iter);
    }
    valueMap.insert(val);
}

and obviously your compare function needs to be updated to account for qty. 很明显,您的比较功能需要更新以说明数量。 It should look something like 它看起来应该像

bool comp(const PriceList& val1, const PriceList& val2)
{
    return make_pair(val1.price, val1.qty) < make_pair(val2.price, val2.qty);
}

You want to do something like like the following. 您想要做类似以下的事情。 Note we only do a single lookup. 请注意,我们仅执行一次查找。

// attempt to insert
std::pair<bool, std::set<PriceLevel>::iterator> result = s1.insert(p1);
if (result.first)  // insert did not work since element already existed
{
    PriceLevel & temp = *(result.second);
    if (temp.isImplied != p1.isImplied)
    {
        temp.qty += p1.qty;  // sum
    }
    else
    {
        temp.qty = p1.qty;  // overwrite
    }
}
// otherwise p1 didn't exist and was inserted

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

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