编写执行与set_symmetric_difference相同的作业的函数
Writing a function that performs the same job as set_symmetric_difference
大家好,很抱歉占用时间! 我正在做在线练习,我的任务是编写一个与set_symmetric_difference
相同的模板函数。该函数采用五个参数p1, p2, p3, p4 p5
。p1 and p2
是第一个块的边界,p3 and p4
第二个块的边界,p5
目标块开头的点。
注意:有三种不同的参数类型,因为 p1 和 p2 可以是指针,而 p3 和 p4 可以是迭代器。
该函数应找到两个集合的对称差,但具有某些条件:
不允许重复(即,如果对称差分集中已经存在值
x
的元素,则不会复制另一个具有相同值的元素)。所有元素必须与原始两个块的顺序相同
第一个块的元素将在第二个块的元素之前复制。
该函数返回一个迭代器/指针,该迭代器/指针不指向目标块的开头,而是指向块中最后一个元素之后的点。
到目前为止,我在解决这个问题方面没有运气。我的解决方案适用于某些实例,但对于其他情况则不起作用。我完全不知道如何遵循规则 1、2 和 3。
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
template <typename type1, typename type2, typename type3>
type3 symmetric(type1 p1, type1 p2, type2 p3, type2 p4, type3 p5) {
sort(p1,p2);
sort(p3,p4);
while(true) {
if(p1 == p2) return copy(p3,p4,p5);
if(p3==p4) return copy(p1,p2,p5);
if(*p1 < *p3) {
*p5=*p1;
p5++;
p1++;
}
else if(*p3 < *p1) {
*p5 = *p3;
p3++;
p5++;
}
else {
p1++;
p3++;
}
}
return p5;
}
int main ()
{
int block1[] = { 5, 2, 7, 4, 6, 1, 3, 2, 7, 4 };
int block2[] = { 2, 9, 0, 6, 0, 4, 8, 3, 2, 5 };
int destination[10];
auto p = symmetric(block1, block1+10, block2, block2+10, destination);
auto destination_begin = destination;
while(destination_begin < p) cout << *destination_begin++;
return 0;
}
对于我提到的示例,输出应该是7 1 9 0 8
但我的程序打印0 0 1 4 7 7 8 9
。我不知道如何解决它。对不起我的无知,如果有人来救我,我会很高兴!感谢一百万次!
首先,您的输出应该被排序。因此,它需要0 1 7 8 9
,而不是7 1 9 0 8
。
代码中缺少的关键逻辑部分是在循环访问输入列表时不会跳过重复的条目。
这是您发布的代码的更新版本,适用于我。
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
template <typename type>
void print(type p1, type p2)
{
while(p1 != p2) cout << *p1++ << " ";
cout << endl;
}
template <typename type>
type skip_duplicates(type p)
{
while ( *p == *(p+1) ) ++p;
return ++p;
}
template <typename type1, typename type2, typename type3>
type3 symmetric(type1 p1, type1 p2, type2 p3, type2 p4, type3 p5) {
sort(p1,p2);
sort(p3,p4);
print(p1, p2);
print(p3, p4);
while(true) {
if(p1 == p2) return copy(p3,p4,p5);
if(p3 == p4) return copy(p1,p2,p5);
if(*p1 < *p3) {
*p5=*p1;
p5++;
p1 = skip_duplicates(p1);
}
else if(*p3 < *p1) {
*p5 = *p3;
p3 = skip_duplicates(p3);
p5++;
}
else {
p1 = skip_duplicates(p1);
p3 = skip_duplicates(p3);
}
}
return p5;
}
int main ()
{
int block1[] = { 5, 2, 7, 4, 6, 1, 3, 2, 7, 4 };
int block2[] = { 2, 9, 0, 6, 0, 4, 8, 3, 2, 5 };
int destination[10];
auto p = symmetric(block1, block1+10, block2, block2+10, destination);
print(destination, p);
return 0;
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- Difference in displaying cv2 Mat
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式