gpt4 book ai didi

c++ - 自定义结构的 set_difference

转载 作者:太空宇宙 更新时间:2023-11-04 16:30:05 35 4
gpt4 key购买 nike

我试图找出以下两组之间的区别:

A = {(0,0), (0,1), (1,0), (1,1), (2,2)}

B = {(0,0), (0,1), (1,0), (1,1)}

我期待的答案是

A - B = {(2,2)}

我尝试了以下代码。但是我无法编译。谁能指出我犯的错误?

#include <vector>
#include <algorithm>
#include <iostream>
#include <utility>

using namespace std;

class compare
{
public:
bool operator()(const pair <int, int> elem1, const pair <int, int> elem2)
{
return ((elem1.first == elem2.first) &&
(elem1.second == elem2.second));
}
};

int main()
{
vector < pair<int, int> > v, va, vb;
va.push_back(make_pair(0,0));
va.push_back(make_pair(0,1));
va.push_back(make_pair(1,0));
va.push_back(make_pair(1,1));
va.push_back(make_pair(2,2));

vb.push_back(make_pair(0,0));
vb.push_back(make_pair(0,1));
vb.push_back(make_pair(1,0));
vb.push_back(make_pair(1,1));


vector < pair<int, int> >::iterator it, result;
result = set_difference (va.begin(), va.end(),
vb.begin(), vb.end(),
inserter(v, v.end()),
compare());

// for (it = v.begin( ) ; it != result; it++)
// cout << "(" << it->first << it->second << ")" << ", ";
// cout << endl;

return 0;
}

编辑:编译错误信息如下:

set_difference.cc: In function `int main()':
set_difference.cc:36: error: no match for 'operator=' in 'result = std::set_difference [with _InputIterator1 = __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > >, _InputIterator2 = __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > >, _OutputIterator = std::insert_iterator<std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > >, _Compare = compare]((&va)->std::vector<_Tp, _Alloc>::begin [with _Tp = std::pair<int, int>, _Alloc = std::allocator<std::pair<int, int> >](), (&va)->std::vector<_Tp, _Alloc>::end [with _Tp = std::pair<int, int>, _Alloc = std::allocator<std::pair<int, int> >](), (&vb)->std::vector<_Tp, _Alloc>::begin [with _Tp = std::pair<int, int>, _Alloc = std::allocator<std::pair<int, int> >](), (&vb)->std::vector<_Tp, _Alloc>::end [with _Tp = std::pair<int, int>, _Alloc = std::allocator<std::pair<int, int> >](), std::inserter [with _Container = std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >, _Iterator = __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > >](((std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >&)(&v)), (&v)->std::vector<_Tp, _Alloc>::end [with _Tp = std::pair<int, int>, _Alloc = std::allocator<std::pair<int, int> >]()), (compare(), compare()))'
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_iterator.h:587: note: candidates are: __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > >& __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > >::operator=(const __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > >&)

最佳答案

(这是解决算法的正确性,而不是编译问题。)

阅读documentation : 两个输入范围已经需要排序

您还必须为结果提供一个输出迭代器

那么这样做:

std::sort(va.begin(), va.end(), compare());
std::sort(vb.begin(), vb.end(), compare());

set_difference(va.begin(), va.end(), vb.begin(), vb.end(),
std::back_inserter(v), compare());

你的 compare函数还应定义严格的弱排序,而不是相等比较。

顺便说一下,std::pair<S, T>std::tuple<T...>已经有了内置的词典比较,所以你通常不需要定义自己的比较,除非你想要一些奇特的东西:std::sort(va.begin(), va.end());

关于c++ - 自定义结构的 set_difference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8073618/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com