Subtraction and Intersection of two vectors of pointers in C++

久未见 提交于 2019-12-23 02:48:12

问题


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??


    回答1:


    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 
    }
    



    回答2:


    If you are using stl, for first problem you can use set_intersection, for second set_difference




    回答3:


    @Amresh, this is the example you wanted using vector with pointer. But I have used boost::shared_ptr instead of raw pointer so you need to worry about memory management:

    class Item;
    typedef boost::shared_ptr<Item> MyPtr;
    typedef std::vector<MyPtr> VecType;
    
    class Item
    {
    public:
        Item(int i):ItemId(i){}
        friend bool operator<(MyPtr lhs, MyPtr rhs);
        friend bool operator==(MyPtr lhs, MyPtr rhs);
    
       int ItemId;
       string ItemDescription;
       float ItemPrice;
    
       bool operator<(const Item& rhs)
       {
           return ItemId < rhs.ItemId;
       }
       bool operator==(const Item& rhs)
       {
           return ItemId == rhs.ItemId;
       }
    };
    
    
    
    bool operator<(MyPtr lhs, MyPtr rhs)
    {
        return lhs->ItemId < rhs->ItemId ;
    }
    
    bool operator==(MyPtr lhs, MyPtr rhs)
    {
        return lhs->ItemId == rhs->ItemId ;
    }
    
    int main()
    {
    
        VecType myvec1;
        myvec1.push_back(MyPtr(new Item(2)));
        myvec1.push_back(MyPtr(new Item(1)));
        myvec1.push_back(MyPtr(new Item(10)));
        myvec1.push_back(MyPtr(new Item(5)));
        myvec1.push_back(MyPtr(new Item(3)));
        myvec1.push_back(MyPtr(new Item(10)));
    
        VecType myvec2;
        myvec2.push_back(MyPtr(new Item(10)));
        myvec2.push_back(MyPtr(new Item(1)));
        myvec2.push_back(MyPtr(new Item(10)));
        myvec2.push_back(MyPtr(new Item(1)));
        myvec2.push_back(MyPtr(new 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());
    
        VecType myvec3; //Intersection of V1 and V2
        VecType 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 
    }
    


    来源:https://stackoverflow.com/questions/6955578/subtraction-and-intersection-of-two-vectors-of-pointers-in-c

  • 标签
    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!