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;
};
Two options to be able to use std::unique
:
-
Define an
operator==
method forPacket
and change thevector<Packet*>
tovector<Packet>
.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<Packet> 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<Packet*>
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<Packet*> 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; }