我正在尝试实现在链接中发现的问题的解决方案。
这是我的代码片段
bool compareVec(vector<int> a, vector<int> b) {
return std::equal(a.begin(), a.end(), b.begin());
}
vector<vector<int> > ans;
ans.erase(std::remove_if(ans.begin(), ans.end(), compareVec), ans.end());我收到以下错误
/usr/include/c++/4.8/bits/stl_algo.h: In instantiation of
'_RandomAccessIterator std::__find_if(_RandomAccessIterator,
_RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with
_RandomAccessIterator = __gnu_cxx::__normal_iterator<std::vector<int>*,
std::vector<std::vector<int> > >; _Predicate = bool (*)(std::vector<int>,
std::vector<int>)]':
/usr/include/c++/4.8/bits/stl_algo.h:4465:41: required from '_IIter
std::find_if(_IIter, _IIter, _Predicate) [with _IIter =
__gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int>
> >; _Predicate = bool (*)(std::vector<int>, std::vector<int>)]'
/usr/include/c++/4.8/bits/stl_algo.h:1144:64: required from '_FIter
std::remove_if(_FIter, _FIter, _Predicate) [with _FIter =
__gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int>
> >; _Predicate = bool (*)(std::vector<int>, std::vector<int>)]'
solution.cpp:40:64: required from here
/usr/include/c++/4.8/bits/stl_algo.h:214:23: error: too few arguments to
function
if (__pred(*__first))
^
/usr/include/c++/4.8/bits/stl_algo.h:218:23: error: too few arguments to
function
if (__pred(*__first))
^
/usr/include/c++/4.8/bits/stl_algo.h:222:23: error: too few arguments to
function
if (__pred(*__first))
^有人能帮我调试一下吗?提前感谢
编辑
对向量的元素进行排序,并对所有这些向量进行排序。
唯一也会给出一个错误。我不知道为什么?
为什么我提供的链接中给出的示例在这里没有帮助?
发布于 2015-12-29 21:32:14
std::remove_if需要一个一元谓词。传递一个二进制谓词,这会导致您的错误(/usr/include/c++/4.8/bits/stl_algo.h:222:23: error: too few arguments to function→您的函数有两个参数,而不是一个)。此外,std::remove_if在删除时不考虑其他元素(这就是它接受一元谓词的原因),因此它实际上不是您要寻找的。
您想要使用的是std::unique,它确实需要您已经实现的compareVec。但是,std::vector已经提供了operator==重载,因此实现实际上是多余的!此外,您还说在使用std::unique时会出现错误。尝试以const&的形式传递您的参数。
因此,当外部向量和内部向量已经排序时,解决方案就像对排序元素的任何其他向量一样:
outer.erase(std::unique(outer.begin(), outer.end()), outer.end());https://stackoverflow.com/questions/34518631
复制相似问题