unique去除重复的向量_std :: unique并从对象的容器中删除重复项

I would like to know if there is an efficient way to remove objects from a container based on values of member fields of the objects. For example, I can do the following using stl::unique with a list of strings:

#include

#include

#include

#include

using namespace std;

bool stringCompare(const string & l, const string & r)

{

return (l==r);

}

int main()

{

list myStrings;

myStrings.push_back("1001");

myStrings.push_back("1001");

myStrings.push_back("81");

myStrings.push_back("1001");

myStrings.push_back("81");

myStrings.sort();

myStrings.erase(unique(myStrings.begin(), myStrings.end(), stringCompare), myStrings.end());

list::iterator it;

for(it = myStrings.begin(); it != myStrings.end(); ++it)

{

cout << *it << endl;

}

return 0;

}

prints 1001, 81...

Is there a way I can do something similar with the following code, or do I need to perform the comparisons "manually" using operators and iterating through the containers. I couldn't think of a more elegant solution and would like to know if this is possible without writing a lot of code. Any help will be much appreciated!

class Packet

{

public:

Packet(string fTime, string rID) : filingTime(fTime), recordID(rID)

string getFilingTime() {return filingTime;}

string getRecordId() {return recordID;}

private:

string filingTime;

string recordID;

};

int main()

{

vector pkts;

pkts.push_back(new Packet("10:20", "1004"));

pkts.push_back(new Packet("10:20", "1004")); // not unique (duplicate of the line above)

pkts.push_back(new Packet("10:20", "251"));

pkts.push_back(new Packet("10:20", "1006"));

// remove packet from vector if time and ID are the same

return 0;

}

Thanks

解决方案

Two options to be able to use std::unique:

Define an operator== method for Packet and change the vector to vector.

bool Packet::operator==(const Packet& rhs) const

{

if (getFilingTime() != rhs.getFilingTime())

return false;

if (getSpid() != rhs.getSpid())

return false;

return true;

}

//etc.

int main()

{

vector pkts;

pkts.push_back(Packet("10:20", "1004"));

pkts.push_back(Packet("10:20", "1004")); // not unique (duplicate of the line above)

pkts.push_back(Packet("10:20", "251"));

pkts.push_back(Packet("10:20", "1006"));

// remove packet from vector if time and ID are the same

pkts.erase(unique(pkts.begin(), pkts.end()), pkts.end());

return 0;

}

Keep the vector as vector and define a method to compare the elements.

bool comparePacketPtrs(Packet* lhs, Packet* rhs)

{

if (lhs->getFilingTime() != rhs->getFilingTime())

return false;

if (lhs->getSpid() != rhs->getSpid())

return false;

return true;

}

//etc.

int main()

{

vector pkts;

pkts.push_back(new Packet("10:20", "1004"));

pkts.push_back(new Packet("10:20", "1004")); // not unique (duplicate of the line above)

pkts.push_back(new Packet("10:20", "251"));

pkts.push_back(new Packet("10:20", "1006"));

// remove packet from vector if time and ID are the same

pkts.erase(unique(pkts.begin(), pkts.end(), comparePacketPtrs), pkts.end());

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值