C++中两个指针向量的减法和交集 [英] Subtraction and Intersection of two vectors of pointers in C++
问题描述
我有两个向量指向我的自定义类对象.
这两个向量中的指针并不指向同一个对象,但存储在对象中的值是相同的.
我的自定义类结构是:
<预><代码>类项目{字符串 ItemId;字符串项目描述;浮动商品价格;}
第一个向量(V1) 有n 个条目,第二个向量(V2) 有m 个条目(n>m).
我必须执行两个操作:
如何有效地做到这一点??
以下示例说明如何使用 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:
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屋!