Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 合并两个std::集_C++_Stl - Fatal编程技术网

C++ 合并两个std::集

C++ 合并两个std::集,c++,stl,C++,Stl,如果价格相同,我需要根据一个成员变量数量将两个集合合并成一个结果集合。在以下示例中,我的结果集s3应包含: 价格:100 数量:40 售价:200 数量:60 请注意,当价格相同时,上述数量是两套各自数量的总和 我的问题是如何构造下面的集合s3: 请用同样的方法指导我 #include <set> #include <iostream> using namespace std; class PriceLevel { public: int price;

如果价格相同,我需要根据一个成员变量数量将两个集合合并成一个结果集合。在以下示例中,我的结果集s3应包含:

价格:100 数量:40

售价:200 数量:60

请注意,当价格相同时,上述数量是两套各自数量的总和

我的问题是如何构造下面的集合s3:

请用同样的方法指导我

#include <set>
#include <iostream>

using namespace std;

class PriceLevel
{
public:
    int price;
    int qty;

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

    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;
    std::set<PriceLevel> s2;

    PriceLevel p1(100,10);
    PriceLevel p2(200,20);

    PriceLevel p3(100,30);
    PriceLevel p4(200,40);

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

    s2.insert(p3);
    s2.insert(p4);

    std::set<PriceLevel> s3;

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

    // How should I Initialize s3

    for(; it != s3.end(); it++)
    {
        cout << "Price: " << it->price << endl;
        cout << "Qty : " << it->qty << endl;
    }
}
#包括
#包括
使用名称空间std;
类别价格水平
{
公众:
国际价格;
整数数量;
价格水平(整数价格、整数数量)
{
价格=_价格;
数量=_数量;
}

朋友BoOL运算符< P> <代码> SET/COM>不是适合您的应用程序的数据结构。请考虑使用<代码> MAP>代码>代替:

map<int, int> p1, p2, p3; // map price -> quantity

p1[100] = 10;
p1[200] = 20;

p2[100] = 30;
p2[200] = 40;

p3 = p1;
for(auto &i : p2) {
    p3[i.first] += i.second;
}

// Now p3[100]=40 and p3[200]=60.
为此,您必须将
数量
声明为
可变整数
,以便即使
价格级别
结构为
常量
(因为
集合
的元素为
常量
),也可以对其进行修改


如果不能使变量<代码>易变的< /代码>,那么您可以尝试删除现有的集合元素,然后添加一个新的合并元素。

< p> <代码> set <代码>不是适合您的应用程序的数据结构。请考虑使用<代码> map <代码>代替:

map<int, int> p1, p2, p3; // map price -> quantity

p1[100] = 10;
p1[200] = 20;

p2[100] = 30;
p2[200] = 40;

p3 = p1;
for(auto &i : p2) {
    p3[i.first] += i.second;
}

// Now p3[100]=40 and p3[200]=60.
为此,您必须将
数量
声明为
可变整数
,以便即使
价格级别
结构为
常量
(因为
集合
的元素为
常量
),也可以对其进行修改


如果无法使变量
可变
,则可以尝试删除现有的set元素,然后添加一个新的合并元素。

您实际上是在尝试使用一个set作为映射,并使用相等的键合并值。您将需要滚动您自己的结果(更不用说这确实是不可取的…).这里有一些东西可以让你开始

#include <iostream>
#include <set>

using namespace std;

class PriceLevel
{
public:
    int price;
    int qty;

    PriceLevel() {
        price = 0;
        qty = 0;
    }

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

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

    //Compares two PriceLevel objects and merges their values if their keys are the same.
    //Return value is a std::pair that
    //denotes if the compare was successful and the result is meaningful.        
    static std::pair<bool, PriceLevel> merge_equal(const PriceLevel& p, const PriceLevel& q) {
        std::pair<bool, PriceLevel> result;
        result.first = false;
        if(p.price == q.price) {
            result.first = true;
            result.second.price = p.price;
            result.second.qty = p.qty + q.qty;
        }
        return result;
    }

};

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

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

    PriceLevel p1(100,10);
    PriceLevel p2(200,20);

    PriceLevel p3(100,30);
    PriceLevel p4(200,40);

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

    s2.insert(p3);
    s2.insert(p4);

    std::set<PriceLevel> s3;

    //Just in case...the world may explode otherwise.
    if(s1.size() == s2.size()) {

        for(const auto& pl1 : s1) {
            for(const auto& pl2 : s2) {
                //Only insert valid values.
                auto r = PriceLevel::merge_equal(pl1, pl2);
                if(r.first) s3.insert(r.second);
            }
        }

        for(auto it = s3.begin(); it != s3.end(); it++) {
            cout << "Price: " << it->price << endl;
            cout << "Qty : " << it->qty << endl;
        }
    }
}
#包括
#包括
使用名称空间std;
类别价格水平
{
公众:
国际价格;
整数数量;
价格水平(){
价格=0;
数量=0;
}
价格水平(整数价格、整数数量)
{
价格=_价格;
数量=_数量;
}

friend bool操作符您实际上是在尝试使用集合作为映射,并使用相等的键合并值。您需要滚动您自己的结果(更不用说这确实是不可取的…)。下面是一些让您开始学习的内容

#include <iostream>
#include <set>

using namespace std;

class PriceLevel
{
public:
    int price;
    int qty;

    PriceLevel() {
        price = 0;
        qty = 0;
    }

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

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

    //Compares two PriceLevel objects and merges their values if their keys are the same.
    //Return value is a std::pair that
    //denotes if the compare was successful and the result is meaningful.        
    static std::pair<bool, PriceLevel> merge_equal(const PriceLevel& p, const PriceLevel& q) {
        std::pair<bool, PriceLevel> result;
        result.first = false;
        if(p.price == q.price) {
            result.first = true;
            result.second.price = p.price;
            result.second.qty = p.qty + q.qty;
        }
        return result;
    }

};

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

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

    PriceLevel p1(100,10);
    PriceLevel p2(200,20);

    PriceLevel p3(100,30);
    PriceLevel p4(200,40);

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

    s2.insert(p3);
    s2.insert(p4);

    std::set<PriceLevel> s3;

    //Just in case...the world may explode otherwise.
    if(s1.size() == s2.size()) {

        for(const auto& pl1 : s1) {
            for(const auto& pl2 : s2) {
                //Only insert valid values.
                auto r = PriceLevel::merge_equal(pl1, pl2);
                if(r.first) s3.insert(r.second);
            }
        }

        for(auto it = s3.begin(); it != s3.end(); it++) {
            cout << "Price: " << it->price << endl;
            cout << "Qty : " << it->qty << endl;
        }
    }
}
#包括
#包括
使用名称空间std;
类别价格水平
{
公众:
国际价格;
整数数量;
价格水平(){
价格=0;
数量=0;
}
价格水平(整数价格、整数数量)
{
价格=_价格;
数量=_数量;
}

friend bool运算符您可以仅用两行合并两个集合

#include <set>

template <typename _Ty>
std::set<_Ty> merge(const std::set<_Ty> &x, const std::set<_Ty> &y) const
{
    std::set<_Ty> merged = x; //initial merged set from x
    merged.insert(y.begin(), y.end()); //add contents of y to merged

    return move(merged);
}
#包括
模板
std::set merge(const std::set&x,const std::set&y)const
{
std::set merged=x;//从x开始的初始合并集
merged.insert(y.begin(),y.end());//将y的内容添加到merged
返回移动(合并);
}

您可以用两行代码合并两个集合

#include <set>

template <typename _Ty>
std::set<_Ty> merge(const std::set<_Ty> &x, const std::set<_Ty> &y) const
{
    std::set<_Ty> merged = x; //initial merged set from x
    merged.insert(y.begin(), y.end()); //add contents of y to merged

    return move(merged);
}
#包括
模板
std::set merge(const std::set&x,const std::set&y)const
{
std::set merged=x;//从x开始的初始合并集
merged.insert(y.begin(),y.end());//将y的内容添加到merged
返回移动(合并);
}

如果您完全确定两个源代码集包含完全相同的价格,则可以使用的二进制版本

如果它们可能包含不相等的数据,则必须手动执行,如下所示:

std::set<PriceLevel> s3;

// How should I Initialize s3
std::set<PriceLevel>::iterator 
    first1 = s1.begin(),
    last1 = s1.end(),
    first2 = s2.begin(),
    last2 = s2.end();
while (first1 != last1 && first2 != last2) {
    if (first1->price < first2->price) {        
        s3.insert(*first1++);
    }
    else if (first1->price > first2->price) {
        s3.insert(*first2++);
    }
    else {
        s3.insert(PriceLevel(first1->price, first1->qty + first2->qty));
        ++first1;
        ++first2;
    }
}
while (first1 != last1) {
     s3.insert(*first1++);
}
while (first2 != last2) {
     s3.insert(*first2++);
}
std::set s3;
//我应该如何初始化s3
std::set::迭代器
first1=s1.begin(),
last1=s1.end(),
first2=s2.begin(),
last2=s2.end();
while(first1!=last1&&first2!=last2){
如果(first1->priceprice){
s3.插入(*first1++);
}
else if(first1->price>first2->price){
s3.插入(*first2++);
}
否则{
s3.插入(PriceLevel(first1->price,first1->qty+first2->qty));
++第一个1;
++前2名;
}
}
while(first1!=last1){
s3.插入(*first1++);
}
while(first2!=last2){
s3.插入(*first2++);
}
这最好放在一个额外的函数中

如果您只需要两个源集中存在的结果集中的价格,则更简单一些:

while (first1 != last1 && first2 != last2) {
    if (first1->price < first2->price) {        
        ++first1;
    }
    else if (first1->price > first2->price) {
        ++first2;
    }
    else {
        s3.insert(PriceLevel(first1->price, first1->qty + first2->qty));
        ++first1;
        ++first2;
    }
}
while(first1!=last1&&first2!=last2){
如果(first1->priceprice){
++第一个1;
}
else if(first1->price>first2->price){
++前2名;
}
否则{
s3.插入(PriceLevel(first1->price,first1->qty+first2->qty));
++第一个1;
++前2名;
}
}

如果您完全确定两个源代码集包含完全相同的价格,则可以使用的二进制版本

如果它们可能包含不相等的数据,则必须手动执行,如下所示:

std::set<PriceLevel> s3;

// How should I Initialize s3
std::set<PriceLevel>::iterator 
    first1 = s1.begin(),
    last1 = s1.end(),
    first2 = s2.begin(),
    last2 = s2.end();
while (first1 != last1 && first2 != last2) {
    if (first1->price < first2->price) {        
        s3.insert(*first1++);
    }
    else if (first1->price > first2->price) {
        s3.insert(*first2++);
    }
    else {
        s3.insert(PriceLevel(first1->price, first1->qty + first2->qty));
        ++first1;
        ++first2;
    }
}
while (first1 != last1) {
     s3.insert(*first1++);
}
while (first2 != last2) {
     s3.insert(*first2++);
}
std::set s3;
//我应该如何初始化s3
std::set::迭代器
first1=s1.begin(),
last1=s1.end(),
first2=s2.begin(),
last2=s2.end();
while(first1!=last1&&first2!=last2){
如果(first1->priceprice){
s3.插入(*first1++);
}
else if(first1->price>first2->price){
s3.插入(*first2++);
}
否则{
s3.插入(PriceLevel(first1->price,first1->qty+first2->qty));
++第一个1;
++前2名;
}
}
while(first1!=last1){
s3.插入(*first1++);
}
while(first2!=last2){
s3.插入(*first2++);
}
这最好放在一个额外的函数中

如果您只需要两个源集中存在的结果集中的价格,则更简单一些:

while (first1 != last1 && first2 != last2) {
    if (first1->price < first2->price) {        
        ++first1;
    }
    else if (first1->price > first2->price) {
        ++first2;
    }
    else {
        s3.insert(PriceLevel(first1->price, first1->qty + first2->qty));
        ++first1;
        ++first2;
    }
}
while(first1!=last1&&first2!=last2){
如果(first1->priceprice){
++第一个1;
}
else if(first1->price>first2->price){
++前2名;
}
否则{
s3.插入(PriceLevel(first1->price,first1->qty+first2->qty));
++第一个1;
++前2名;
}
}

是否必须使用
std::set