C++ 中两个指针向量的减法和交集

发布于 2024-11-28 14:19:04 字数 692 浏览 0 评论 0原文

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

我的自定义类结构是:


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).

我必须执行两项操作:

  • 获取一个在V1V2中都有共同对象的向量。 通过共同点,我的意思是说元素的 ItemId 是相同的。(可以称为V1 和 V2 的交集)。< p>

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

    如何有效地做到这一点?

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

  • 如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    最终幸福 2024-12-05 14:19:04

    以下是如何使用 STL set_intersection 和 set_difference 来获得您想要的内容的示例:

    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 
    }
    

    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 
    }
    
    筱果果 2024-12-05 14:19:04

    如果您使用的是stl,对于第一个问题,您可以使用set_intersection,对于第二个问题< a href="http://www.sgi.com/tech/stl/set_difference.html" rel="nofollow">set_difference

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

    ι不睡觉的鱼゛ 2024-12-05 14:19:04

    @Amresh,这是您想要使用带有指针的向量的示例。但我使用了 boost::shared_ptr 而不是原始指针,所以你需要担心内存管理:

    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 
    }
    

    @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 
    }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文