C++中两个指针向量的减法和交集 [英] Subtraction and Intersection of two vectors of pointers in C++

查看:24
本文介绍了C++中两个指针向量的减法和交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个向量指向我的自定义类对象.
这两个向量中的指针并不指向同一个对象,但存储在对象中的值是相同的.

我的自定义类结构是:
<预><代码>类项目{字符串 ItemId;字符串项目描述;浮动商品价格;}

第一个向量(V1) 有n 个条目,第二个向量(V2) 有m 个条目(n>m).

我必须执行两个操作:

  • 获取在 V1V2 中具有共同对象的向量.通用,我的意思是说元素的 ItemId 是相同的.(可以称为 V1 和 V2 的交集).

  • 获取一个向量,其中包含 V2 中不存在的元素.(可以称为V1-V2).

    如何有效地做到这一点??

    解决方案

    以下示例说明如何使用 STL set_intersection 和 set_difference 来实现您想要的:

    类项目{民众:项目(int i):ItemId(i){}INT项目ID;字符串项目描述;浮动商品价格;bool 运算符<(const Item& rhs){返回 ItemId myvec1;myvec1.push_back(Item(1));myvec1.push_back(Item(2));myvec1.push_back(Item(1));myvec1.push_back(Item(10));myvec1.push_back(Item(5));myvec1.push_back(Item(3));myvec1.push_back(Item(10));std::vector<项目>myvec2;myvec2.push_back(Item(10));myvec2.push_back(Item(1));myvec2.push_back(Item(10));myvec2.push_back(Item(1));myvec2.push_back(Item(3));std::sort(myvec1.begin(), myvec1.end());std::sort(myvec2.begin(), myvec2.end());myvec1.erase(std::unique(myvec1.begin(), myvec1.end()), myvec1.end());myvec2.erase(std::unique(myvec2.begin(), myvec2.end()), myvec2.end());std::vector<项目>myvec3;//V1和V2的交点std::vector<项目>myvec4;//V1-V2set_intersection(myvec1.begin(),myvec1.end(),myvec2.begin(),myvec2.end(),std::back_inserter(myvec3));//myvec3: 1 3 10set_difference(myvec1.begin(),myvec1.end(),myvec2.begin(),myvec2.end(),std::back_inserter(myvec4));//myvec4: 2 5}

    I've two vectors having pointers to my custom class object.
    The pointers in these two vectors don't point to the same object, but the values stored in the objects are same.

    My custom class structure is:

    
    Class Item
    {
       string ItemId;
       string ItemDescription;
       float ItemPrice;
    }
    

    The first vector(V1) is having n entries and the second vector(V2) is having m entries (n>m).

    I've to perform two operations:

  • Get a vector which has common objects in both V1 and V2. By common, I mean to say that the ItemId for the elements is same. (Can be referred as Intersection of V1 and V2).

  • Get a vector which has the elements which are not present in V2. (Can be referred as V1-V2).

    How to do this in an efficient manner??

    解决方案

    Here is example of how to do it using STL set_intersection and set_difference to get what you wanted:

    class Item
    {
    public:
        Item(int i):ItemId(i){}
    
       int ItemId;
       string ItemDescription;
       float ItemPrice;
    
       bool operator<(const Item& rhs)
       {
           return ItemId < rhs.ItemId;
       }
       bool operator==(const Item& rhs)
       {
           return ItemId == rhs.ItemId;
       }
    };
    
    int main()
    {
        std::vector<Item> myvec1;
        myvec1.push_back(Item(1));
        myvec1.push_back(Item(2));
        myvec1.push_back(Item(1));
        myvec1.push_back(Item(10));
        myvec1.push_back(Item(5));
        myvec1.push_back(Item(3));
        myvec1.push_back(Item(10));
    
    
        std::vector<Item> myvec2;
        myvec2.push_back(Item(10));
        myvec2.push_back(Item(1));
        myvec2.push_back(Item(10));
        myvec2.push_back(Item(1));
        myvec2.push_back(Item(3));
    
        std::sort(myvec1.begin(), myvec1.end());
        std::sort(myvec2.begin(), myvec2.end());
    
        myvec1.erase(std::unique(myvec1.begin(), myvec1.end()), myvec1.end());
        myvec2.erase(std::unique(myvec2.begin(), myvec2.end()), myvec2.end());
    
        std::vector<Item> myvec3; //Intersection of V1 and V2
        std::vector<Item> myvec4; //V1-V2
        set_intersection(myvec1.begin(),myvec1.end(), myvec2.begin(),myvec2.end(), std::back_inserter(myvec3)); //myvec3: 1 3 10
        set_difference(myvec1.begin(),myvec1.end(), myvec2.begin(),myvec2.end(), std::back_inserter(myvec4));   //myvec4: 2 5 
    }
    

    这篇关于C++中两个指针向量的减法和交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆