这是一个家庭作业,我真的不知道该怎么做。我通常会创建 v3 = v1,然后通过 v2、通过 i 递增,并检查 v2 的元素是否在 v3 中。如果没有,我会将它们添加到 v3。但是我无法在方法之外创建 v3,如果我尝试在方法内部创建它,它只会自行重置。任何人都可以帮助我吗?
这是迄今为止我为它们提供的代码及其包装函数(它只是骨架):
// returns a new vector; every element in v1 and every element in v2 are also in this new vector
// if an element appears in both v1 and v2, it is only added once to the new vector
template <typename T> vector<T> vec_union(vector<T> &v1, vector<T> &v2)
{
return v1;
}
template <typename T> vector<T> vec_union(vector<T> &v1, vector<T> &v2, unsigned i)
{
return v1;
}
// returns a new vector; every element that is in both v1 and v2 are also in this new vector
// there are no duplicates in v1 and v2
template <typename T> vector<T> intersection(vector<T> v1, vector<T> v2)
{
return v1;
}
template <typename T> vector<T> intersection(vector<T> v1, vector<T> v2, unsigned i)
{
return v1;
}
// returns a new vector; every element that is in v1 but not v2 are also in this new vector
// there are no duplicates in v1 and v2
template <typename T> vector<T> difference(vector<T> v1, vector<T> v2)
{
return v1;
}
template <typename T> vector<T> difference(vector<T> v1, vector<T> v2, unsigned i)
{
return v1;
}