- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图找出以下两组之间的区别:
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/
我试图找出以下两组之间的区别: A = {(0,0), (0,1), (1,0), (1,1), (2,2)} B = {(0,0), (0,1), (1,0), (1,1)} 我期待的答案是 A -
在任务中有人说有必要从数组中获取最大元素的“值”。然后将它与第二个数组进行比较,并排除重复项。 任务中需要在vector中使用partial_sort_copy,以及set_difference。 问
set_difference算法需要以下内容 The elements in the ranges shall already be ordered according to this same cr
我想问一下是否可以提供一个示例,说明如何使用 set_difference 找出 map 的集合和键之间的区别 我知道另一个问题std::set_difference is it possible t
我对 中的 set_differences 函数有一个奇怪的问题 header 。我试图逐行读取两个文本文件,并将每一行放入相应的集合中。由于某种原因,set-difference 没有检测到两组之
std::set tradedSymbolSet; //.. // tradedSymbols is filled //.. std::set symbols; //... // symbols is
所以我们得到了一组新的字符串,我们有一个作为映射键。我们想用一种方式 set_difference(注意 - 不是 set_symmetric_difference)。所以目前我有这样丑陋的代码:
我正在尝试调用 set_difference 函数,并将结果放在 std::list 中。理论上,可以在任何排序的容器上执行此操作,对吧? list v; list l1; list
我想知道标准库中是否有任何工具可以同时计算两个排序范围之间的交集和差集。带有以下签名的东西: template Output3 decompose_sets (Input1 first1, Inpu
我实现了 set_union、set_intersection 和 set_difference 的版本,它们接受一个排序的容器和一个排序的范围(不能在容器内), 并将运算结果写入容器。 templa
目前我正在获取两个 map 的集合差异,然后遍历生成的 map 。 理想情况下,我想创建一个差异 vector 而不是 map 。这样我可以更有效地进行迭代。 typedef std::map Reg
以下是STL algorithm的几个函数,使用的条件是有序容器,所以 vector在被sort了之后是可以使用的,set也是可以使用的。 set_difference 这个是求得在第一个容器中有
我在从 set_difference() 获得预期结果时遇到了一些问题.我以为我正在比较两个动态数组,但我不确定差距在哪里。我唯一的额外见解是,当我使用 gettype() 比较两个数组时函数,我得到
我试图找到两个 vector 的集合差异,所以我做了这样的事情: std::vector first_vec, second_vec, difference_vec; // populate firs
如果是,那是什么? 编辑:回应以下评论: var tabulatedOutputErrors = from error in outputErrors
我们能否像STL中的set_intersection和set_difference一样实现multimap_intersection或multimap_difference函数? 最佳答案 这个问题很
我有如下代码: typedef std::set set_of_strings; set_of_strings s1, s2, result1; some_func()
我是一名优秀的程序员,十分优秀!