开发者

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

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 
    }
    


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


    @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 
    }
    
  • 0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜