C++ 中两个指针向量的减法和交集
我有两个向量,它们都有指向我的自定义类对象的指针。
这两个向量中的指针并不指向同一个对象,但对象中存储的值是相同的。
我的自定义类结构是:
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 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:
How to do this in an efficient manner??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是如何使用 STL set_intersection 和 set_difference 来获得您想要的内容的示例:
Here is example of how to do it using STL set_intersection and set_difference to get what you wanted:
如果您使用的是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
@Amresh,这是您想要使用带有指针的向量的示例。但我使用了 boost::shared_ptr 而不是原始指针,所以你需要担心内存管理:
@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: