首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从向量向量中删除重复向量

从向量向量中删除重复向量
EN

Stack Overflow用户
提问于 2015-12-29 20:32:16
回答 1查看 1.6K关注 0票数 0

我正在尝试实现在链接中发现的问题的解决方案。

这是我的代码片段

代码语言:javascript
复制
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());

我收到以下错误

代码语言:javascript
复制
/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))
                   ^

有人能帮我调试一下吗?提前感谢

编辑

对向量的元素进行排序,并对所有这些向量进行排序。

唯一也会给出一个错误。我不知道为什么?

为什么我提供的链接中给出的示例在这里没有帮助?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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&的形式传递您的参数。

因此,当外部向量和内部向量已经排序时,解决方案就像对排序元素的任何其他向量一样:

代码语言:javascript
复制
outer.erase(std::unique(outer.begin(), outer.end()), outer.end());
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34518631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档