删除向量中的重复项 [英] remove duplicates entry in vectors

查看:68
本文介绍了删除向量中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用class(Sample)添加对象,对向量进行排序,然后删除向量中的重复项.

I am trying to add objects using class(Sample),sort my vector and after that remove duplicates entries in my vector.

我的代码(这只是我程序中代码的一部分)

my codes (this is just part of my codes in my program)

vector<Sample> sampleVector;
sort(sampleVector.begin(), sampleVector.end());
sampleVector.erase(std::unique(sampleVector.begin(),sampleVector.end(),sampleVector.end()));

但是当我尝试运行程序时却显示此错误.

but however it when I tried to run my program it shows this error.

Type 'std::__1::__wrap_iter<Sample *>' does not provide a call operator

并且我意识到错误很可能是由这一行引起的

and I realized that most likely the error is caused by this line

sampleVector.erase(std::unique(sampleVector.begin(),sampleVector.end(),sampleVector.end()));

我应该怎么做才能使它删除向量中的重复条目?预先感谢

What should I do so that I can make it work to remove duplicate entries in my vector? thanks in advance

我尝试过的另一件事,但是没有用.

Another thing I have tried but it's not working.

bool myfunction (Sample *i,Sample *j) {
   return (i==j);
}

std::vector<Sample>::iterator it;
vector<Sample> sampleVector;
it = std::unique(sampleVector.begin(), sampleVector.end(),myfunction);   
for (it=sampleVector.begin(); it!=sampleVector.end(); ++it) {
            std::cout << *it << " "; <-- error must change it to &*it
}

推荐答案

括号错误.更正:

sampleVector.erase( std::unique(sampleVector.begin(),sampleVector.end()),
                    sampleVector.end() );

我不怪你被赶上了.C ++编译器错误令人发指.

I don't blame you for getting caught out. C++ compiler errors are heinous.

这篇关于删除向量中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆